Fix for texinfo10.test.
[platform/upstream/automake.git] / automake.in
1 #!@PERL@ -w
2 # -*- perl -*-
3 # @configure_input@
4
5 eval 'exec @PERL@ -S $0 ${1+"$@"}'
6     if 0;
7
8 # automake - create Makefile.in from Makefile.am
9 # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
10 # Free Software Foundation, Inc.
11
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2, or (at your option)
15 # any later version.
16
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.
21
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 # 02111-1307, USA.
26
27 # Originally written by David Mackenzie <djm@gnu.ai.mit.edu>.
28 # Perl reimplementation by Tom Tromey <tromey@cygnus.com>.
29
30 package Language;
31
32 BEGIN
33 {
34   my $prefix = "@prefix@";
35   my $perllibdir = $ENV{'perllibdir'} || "@datadir@/@PACKAGE@";
36   unshift @INC, "$perllibdir";
37 }
38
39 use Automake::Struct;
40 struct (# Short name of the language (c, f77...).
41         'name' => '$',
42         # Nice name of the language (C, Fortran 77...).
43         'Name' => '$',
44
45         # List of configure variables which must be defined.
46         'config_vars' => '@',
47
48         'ansi'    => '$',
49         # `pure' is `1' or `'.  A `pure' language is one where, if
50         # all the files in a directory are of that language, then we
51         # do not require the C compiler or any code to call it.
52         'pure'   => '$',
53
54         'autodep' => '$',
55
56         # Name of the compiling variable (COMPILE).
57         'compiler'  => '$',
58         # Content of the compiling variable.
59         'compile'  => '$',
60         # Flag to require compilation without linking (-c).
61         'compile_flag' => '$',
62         'derived_autodep' => '$',
63         'extensions'      => '@',
64         'flags' => '$',
65
66         # Name of the linking variable (LINK).
67         'linker' => '$',
68         # Content of the linking variable.
69         'link' => '$',
70
71         # Name of the linker variable (LD).
72         'lder' => '$',
73         # Content of the linker variable ($(CC)).
74         'ld' => '$',
75
76         # Flag to specify the output file (-o).
77         'output_flag' => '$',
78         '_finish' => '$');
79
80
81 sub finish ($)
82 {
83   my ($self) = @_;
84   if (defined $self->_finish)
85     {
86       &{$self->_finish} ();
87     }
88 }
89
90
91 package Automake;
92
93 require 5.005;
94 use strict 'vars', 'subs';
95 use File::Basename;
96 use IO::File;
97
98 my $me = basename ($0);
99
100
101 ## ----------- ##
102 ## Constants.  ##
103 ## ----------- ##
104
105 # Parameters set by configure.  Not to be changed.  NOTE: assign
106 # VERSION as string so that eg version 0.30 will print correctly.
107 my $VERSION = "@VERSION@";
108 my $PACKAGE = "@PACKAGE@";
109 my $prefix = "@prefix@";
110 my $libdir = "@datadir@/@PACKAGE@";
111
112 # String constants.
113 my $IGNORE_PATTERN = '^\s*##([^#\n].*)?\n';
114 my $WHITE_PATTERN = '^\s*$';
115 my $COMMENT_PATTERN = '^#';
116 my $TARGET_PATTERN='[$a-zA-Z_.][-.a-zA-Z0-9_(){}/$]*';
117 # A rule has three parts: a list of targets, a list of dependencies,
118 # and optionally actions.
119 my $RULE_PATTERN =
120   "^($TARGET_PATTERN(?:(?:\\\\\n|\\s)+$TARGET_PATTERN)*) *:([^=].*|)\$";
121
122 my $SUFFIX_RULE_PATTERN = '^\.([a-zA-Z0-9]+)\.([a-zA-Z0-9]+)$';
123 # Only recognize leading spaces, not leading tabs.  If we recognize
124 # leading tabs here then we need to make the reader smarter, because
125 # otherwise it will think rules like `foo=bar; \' are errors.
126 my $MACRO_PATTERN = '^[A-Za-z0-9_@]+$';
127 my $ASSIGNMENT_PATTERN = '^ *([^ \t=]*)\s*([:+]?)=\s*(.*)$';
128 # This pattern recognizes a Gnits version id and sets $1 if the
129 # release is an alpha release.  We also allow a suffix which can be
130 # used to extend the version number with a "fork" identifier.
131 my $GNITS_VERSION_PATTERN = '\d+\.\d+([a-z]|\.\d+)?(-[A-Za-z0-9]+)?';
132 my $IF_PATTERN =          '^if\s+(!?)\s*([A-Za-z][A-Za-z0-9_]*)\s*(?:#.*)?$';
133 my $ELSE_PATTERN =   '^else(?:\s+(!?)\s*([A-Za-z][A-Za-z0-9_]*))?\s*(?:#.*)?$';
134 my $ENDIF_PATTERN = '^endif(?:\s+(!?)\s*([A-Za-z][A-Za-z0-9_]*))?\s*(?:#.*)?$';
135 my $PATH_PATTERN='(\w|[/.-])+';
136 # This will pass through anything not of the prescribed form.
137 my $INCLUDE_PATTERN = ('^include\s+'
138                        . '((\$\(top_srcdir\)/' . $PATH_PATTERN . ')'
139                        . '|(\$\(srcdir\)/' . $PATH_PATTERN . ')'
140                        . '|([^/\$]' . $PATH_PATTERN. '))\s*(#.*)?$');
141
142 # Some regular expressions.  One reason to put them here is that it
143 # makes indentation work better in Emacs.
144 my $AC_CONFIG_AUX_DIR_PATTERN = 'AC_CONFIG_AUX_DIR\(([^)]+)\)';
145 my $AM_INIT_AUTOMAKE_PATTERN = 'AM_INIT_AUTOMAKE\([^,]*,([^,)]+)[,)]';
146 my $AM_PACKAGE_VERSION_PATTERN = '^\s*\[?([^]\s]+)\]?\s*$';
147 # Note that there is no AC_PATH_TOOL.  But we don't really care.
148 my $AC_CHECK_PATTERN = 'AC_(CHECK|PATH)_(PROG|PROGS|TOOL)\(\[?(\w+)';
149 my $AM_MISSING_PATTERN = 'AM_MISSING_PROG\(\[?(\w+)';
150 # Just check for alphanumeric in AC_SUBST.  If you do AC_SUBST(5),
151 # then too bad.
152 my $AC_SUBST_PATTERN = 'AC_SUBST\(\[?(\w+)';
153 my $AM_CONDITIONAL_PATTERN = 'AM_CONDITIONAL\(\[?(\w+)';
154
155 # Constants to define the "strictness" level.
156 my $FOREIGN = 0;
157 my $GNU = 1;
158 my $GNITS = 2;
159
160 # Values for AC_CANONICAL_*
161 my $AC_CANONICAL_HOST = 1;
162 my $AC_CANONICAL_SYSTEM = 2;
163
164 # Values indicating when something should be cleaned.  Right now we
165 # only need to handle `mostly'- and `dist'-clean; add more as
166 # required.
167 my $MOSTLY_CLEAN = 0;
168 my $DIST_CLEAN = 1;
169
170 # Files installed by libtoolize.
171 my @libtoolize_files = ('ltmain.sh', 'config.guess', 'config.sub');
172 # ltconfig appears here for compatibility with old versions of libtool.
173 my @libtoolize_sometimes = ('ltconfig', 'ltcf-c.sh', 'ltcf-cxx.sh',
174                             'ltcf-gcj.sh');
175
176 # Commonly found files we look for and automatically include in
177 # DISTFILES.
178 my @common_files =
179   (
180    'README', 'THANKS', 'TODO', 'NEWS', 'COPYING', 'COPYING.LIB',
181    'INSTALL', 'ABOUT-NLS', 'ChangeLog', 'configure.ac',
182    'configure.in', 'configure', 'config.guess', 'config.sub',
183    'AUTHORS', 'BACKLOG', 'ABOUT-GNU', 'libversion.in',
184    'mdate-sh', 'mkinstalldirs', 'install-sh', 'texinfo.tex',
185    'ansi2knr.c', 'ansi2knr.1', 'elisp-comp',
186    # ltconfig appears here for compatibility with old versions
187    # of libtool.
188    'ylwrap', 'acinclude.m4', @libtoolize_files, @libtoolize_sometimes,
189    'missing', 'depcomp', 'compile', 'py-compile'
190   );
191
192 # Commonly used files we auto-include, but only sometimes.
193 my @common_sometimes =
194   (
195    'aclocal.m4', 'acconfig.h', 'config.h.top',
196    'config.h.bot', 'stamp-h.in', 'stamp-vti'
197   );
198
199 # Copyright on generated Makefile.ins.
200 my $gen_copyright = "\
201 # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
202 # Free Software Foundation, Inc.
203 # This Makefile.in is free software; the Free Software Foundation
204 # gives unlimited permission to copy and/or distribute it,
205 # with or without modifications, as long as this notice is preserved.
206
207 # This program is distributed in the hope that it will be useful,
208 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
209 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
210 # PARTICULAR PURPOSE.
211 ";
212
213 # These constants are returned by lang_*_rewrite functions.
214 # LANG_SUBDIR means that the resulting object file should be in a
215 # subdir if the source file is.  In this case the file name cannot
216 # have `..' components.
217 my $LANG_IGNORE = 0;
218 my $LANG_PROCESS = 1;
219 my $LANG_SUBDIR = 2;
220
221 # Directories installed during 'install-exec' phase.
222 my %exec_dir_p =
223   (
224    'bin'        => 1,
225    'sbin'       => 1,
226    'libexec'    => 1,
227    'data'       => 0,
228    'sysconf'    => 1,
229    'localstate' => 1,
230    'lib'        => 1,
231    'info'       => 0,
232    'man'        => 0,
233    'include'    => 0,
234    'oldinclude' => 0,
235    'pkgdata'    => 0,
236    'pkglib'     => 1,
237    'pkginclude' => 0
238   );
239
240 # Map from obsolete macros to hints for new macros.
241 # If you change this, change the corresponding list in aclocal.in.
242 # FIXME: should just put this into a single file.
243 my %obsolete_macros =
244     (
245      'AC_FEATURE_CTYPE'         => "use `AC_HEADER_STDC'",
246      'AC_FEATURE_ERRNO'         => "add `strerror' to `AC_REPLACE_FUNCS(...)'",
247      'AC_FEATURE_EXIT'          => '',
248      'AC_SYSTEM_HEADER'         => '',
249
250      # Note that we do not handle this one, because it is still run
251      # from AM_CONFIG_HEADER.  So we deal with it specially in
252      # &scan_autoconf_files.
253      # 'AC_CONFIG_HEADER'       => "use `AM_CONFIG_HEADER'",
254
255      'fp_C_PROTOTYPES'          => "use `AM_C_PROTOTYPES'",
256      'fp_PROG_CC_STDC'          => "use `AM_PROG_CC_STDC'",
257      'fp_PROG_INSTALL'          => "use `AC_PROG_INSTALL'",
258      'fp_WITH_DMALLOC'          => "use `AM_WITH_DMALLOC'",
259      'fp_WITH_REGEX'            => "use `AM_WITH_REGEX'",
260      'gm_PROG_LIBTOOL'          => "use `AM_PROG_LIBTOOL'",
261      'jm_MAINTAINER_MODE'       => "use `AM_MAINTAINER_MODE'",
262      'md_TYPE_PTRDIFF_T'        => "use `AM_TYPE_PTRDIFF_T'",
263      'ud_PATH_LISPDIR'          => "use `AM_PATH_LISPDIR'",
264      'ud_GNU_GETTEXT'           => "use `AM_GNU_GETTEXT'",
265
266      # Now part of autoconf proper, under a different name.
267      'AM_FUNC_FNMATCH'          => "use `AC_FUNC_FNMATCH'",
268      'fp_FUNC_FNMATCH'          => "use `AC_FUNC_FNMATCH'",
269      'AM_SANITY_CHECK_CC'       => "automatically done by `AC_PROG_CC'",
270      'AM_PROG_INSTALL'          => "use `AC_PROG_INSTALL'",
271      'AM_EXEEXT'                => "use `AC_EXEEXT'",
272      'AM_CYGWIN32'              => "use `AC_CYGWIN'",
273      'AM_MINGW32'               => "use `AC_MINGW32'",
274      'AM_FUNC_MKTIME'           => "use `AC_FUNC_MKTIME'",
275
276 # These aren't quite obsolete.
277 #      'md_PATH_PROG',
278      );
279
280 # Regexp to match the above macros.
281 my $obsolete_rx = '(\b' . join ('\b|\b', keys %obsolete_macros) . '\b)';
282 \f
283
284
285 ## ---------------------------------- ##
286 ## Variables related to the options.  ##
287 ## ---------------------------------- ##
288
289 # TRUE if we should always generate Makefile.in.
290 my $force_generation = 1;
291
292 # Strictness level as set on command line.
293 my $default_strictness = $GNU;
294
295 # Name of strictness level, as set on command line.
296 my $default_strictness_name = 'gnu';
297
298 # This is TRUE if automatic dependency generation code should be
299 # included in generated Makefile.in.
300 my $cmdline_use_dependencies = 1;
301
302 # TRUE if in verbose mode.
303 my $verbose = 0;
304
305 # This holds our (eventual) exit status.  We don't actually exit until
306 # we have processed all input files.
307 my $exit_status = 0;
308
309 # From the Perl manual.
310 my $symlink_exists = (eval 'symlink ("", "");', $@ eq '');
311
312 # TRUE if missing standard files should be installed.
313 my $add_missing = 0;
314
315 # TRUE if we should copy missing files; otherwise symlink if possible.
316 my $copy_missing = 0;
317
318 # TRUE if we should always update files that we know about.
319 my $force_missing = 0;
320
321
322 ## ---------------------------------------- ##
323 ## Variables filled during files scanning.  ##
324 ## ---------------------------------------- ##
325
326 # Name of the top autoconf input: `configure.ac' or `configure.in'.
327 my $configure_ac = '';
328
329 # Files found by scanning configure.ac for LIBOBJS.
330 my %libsources = ();
331
332 # True if AM_C_PROTOTYPES appears in configure.ac.
333 my $am_c_prototypes = 0;
334
335 # Names used in AC_CONFIG_HEADER call.  @config_fullnames holds the
336 # name which appears in AC_CONFIG_HEADER, colon and all.
337 # @config_names holds the file names.  @config_headers holds the '.in'
338 # files.  Ordinarily these are similar, but they can be different if
339 # the weird "NAME:FILE" syntax is used.
340 my @config_fullnames = ();
341 my @config_names = ();
342 my @config_headers = ();
343 # Line number at which AC_CONFIG_HEADER appears in configure.ac.
344 my $config_header_line = 0;
345
346 # Directory where output files go.  Actually, output files are
347 # relative to this directory.
348 my $output_directory = '.';
349
350 # List of Makefile.am's to process, and their corresponding outputs.
351 my @input_files = ();
352 my %output_files = ();
353
354 # Complete list of Makefile.am's that exist.
355 my @configure_input_files = ();
356
357 # List of files in AC_OUTPUT without Makefile.am, and their outputs.
358 my @other_input_files = ();
359 # Line number at which AC_OUTPUT seen.
360 my $ac_output_line = 0;
361
362 # List of directories to search for configure-required files.  This
363 # can be set by AC_CONFIG_AUX_DIR.
364 my @config_aux_path = ('.', '..', '../..');
365 my $config_aux_dir = '';
366 my $config_aux_dir_set_in_configure_in = 0;
367
368 # Whether AM_GNU_GETTEXT has been seen in configure.ac.
369 my $seen_gettext = 0;
370 # Line number at which AM_GNU_GETTEXT seen.
371 my $ac_gettext_line = 0;
372
373 # Whether ALL_LINGUAS has been seen.
374 my $seen_linguas = '';
375 # The actual text.
376 my $all_linguas = '';
377 # Line number at which it appears.
378 my $all_linguas_line = 0;
379
380 # TRUE if AC_DECL_YYTEXT was seen.
381 my $seen_decl_yytext = 0;
382
383 # TRUE if we've seen AC_CANONICAL_(HOST|SYSTEM).  The presence of
384 # AC_CHECK_TOOL also sets this.
385 my $seen_canonical = 0;
386
387 # TRUE if we've seen AC_PROG_LIBTOOL.
388 my $seen_libtool = 0;
389 my $libtool_line = 0;
390
391 # TRUE if we've seen AM_MAINTAINER_MODE.
392 my $seen_maint_mode = 0;
393
394 # Actual version we've seen.
395 my $package_version = '';
396
397 # Line number where we saw version definition.
398 my $package_version_line = 0;
399
400 # TRUE if we've seen AM_PATH_LISPDIR.
401 my $seen_lispdir = 0;
402
403 # TRUE if we've seen AM_PATH_PYTHON.
404 my $seen_pythondir = 0;
405
406 # TRUE if we've seen AC_EXEEXT.
407 my $seen_exeext = 0;
408
409 # TRUE if we've seen AC_OBJEXT.
410 my $seen_objext = 0;
411
412 # TRUE if we've seen AC_ENABLE_MULTILIB.
413 my $seen_multilib = 0;
414
415 # TRUE if we've seen AM_PROG_CC_C_O
416 my $seen_cc_c_o = 0;
417
418 # TRUE if we've seen AM_INIT_AUTOMAKE.
419 my $seen_init_automake = 0;
420
421 # Hash table of discovered configure substitutions.  Keys are names,
422 # values are `FILE:LINE' strings which are used by error message
423 # generation.
424 my %configure_vars = ();
425
426 # This is used to keep track of which variable definitions we are
427 # scanning.  It is only used in certain limited ways, but it has to be
428 # global.  It is declared just for documentation purposes.
429 my %vars_scanned = ();
430
431 # Charsets used by maintainer and in distribution.  MAINT_CHARSET is
432 # handled in a funny way: if seen in the top-level Makefile.am, it is
433 # used for every directory which does not specify a different value.
434 # The rationale here is that some directories (eg gettext) might be
435 # distributions of other packages, and thus require their own charset
436 # info.  However, the DIST_CHARSET must be the same for the entire
437 # package; it can only be set at top-level.
438 # FIXME: this yields bugs when rebuilding.  What to do?  Always
439 # read (and sometimes discard) top-level Makefile.am?
440 my $maint_charset = '';
441 my $dist_charset = 'utf8';              # recode doesn't support this yet.
442
443 # TRUE if --cygnus seen.
444 my $cygnus_mode = 0;
445
446 # Hash table of AM_CONDITIONAL variables seen in configure.
447 my %configure_cond = ();
448
449 # This maps extensions onto language names.
450 my %extension_map = ();
451
452 # List of the DIST_COMMON files we discovered while reading
453 # configure.in
454 my $configure_dist_common = '';
455
456 # This maps languages names onto objects.
457 my %languages = ();
458
459 # List of targets we must always output.
460 # FIXME: Complete, and remove falsely required targets.
461 my %required_targets =
462   (
463    'all'          => 1,
464    'dvi'          => 1,
465    'info'         => 1,
466    'install-info' => 1,
467    'install'      => 1,
468    'install-data' => 1,
469    'install-exec' => 1,
470    'uninstall'    => 1,
471
472    # FIXME: Not required, temporary hacks.
473    # Well, actually they are sort of required: the -recursive
474    # targets will run them anyway...
475    'dvi-am'          => 1,
476    'info-am'         => 1,
477    'install-data-am' => 1,
478    'install-exec-am' => 1,
479    'installcheck-am' => 1,
480    'uninstall-am' => 1,
481
482    'install-man' => 1,
483   );
484
485 \f
486
487 ################################################################
488
489 ## ------------------------------------------ ##
490 ## Variables reset by &initialize_per_input.  ##
491 ## ------------------------------------------ ##
492
493 # Basename and relative dir of the input file.
494 my $am_file_name;
495 my $am_relative_dir;
496
497 # Same but wrt Makefile.in.
498 my $in_file_name;
499 my $relative_dir;
500
501 # These two variables are used when generating each Makefile.in.
502 # They hold the Makefile.in until it is ready to be printed.
503 my $output_rules;
504 my $output_vars;
505 my $output_trailer;
506 my $output_all;
507 my $output_header;
508
509 # Suffixes found during a run.
510 my @suffixes;
511
512 # Handling the variables.
513 #
514 # For a $VAR:
515 # - $var_value{$VAR}{$COND} is its value associated to $COND,
516 # - $var_line{$VAR} is where it has been defined,
517 # - $var_comment{$VAR} are the comments associated to it.
518 # - $var_type{$VAR} is how it has been defined (`', `+', or `:'),
519 # - $var_is_am{$VAR} is true if the variable is owned by Automake.
520 my %var_value;
521 my %var_line;
522 my %var_comment;
523 my %var_type;
524 my %var_is_am;
525
526 # This holds a 1 if a particular variable was examined.
527 my %content_seen;
528
529 # This holds the names which are targets.  These also appear in
530 # %contents.
531 my %targets;
532
533 # Same as %VAR_VALUE, but for targets.
534 my %target_conditional;
535
536 # This is the conditional stack.
537 my @cond_stack;
538
539 # This holds the set of included files.
540 my @include_stack;
541
542 # This holds a list of directories which we must create at `dist'
543 # time.  This is used in some strange scenarios involving weird
544 # AC_OUTPUT commands.
545 my %dist_dirs;
546
547 # List of dependencies for the obvious targets.
548 my @all;
549 my @check;
550 my @check_tests;
551
552 # Holds the dependencies of targets which dependencies are factored.
553 # Typically, `.PHONY' will appear in plenty of *.am files, but must
554 # be output once.  Arguably all pure dependencies could be subject
555 # to this factorization, but it is not unpleasant to have paragraphs
556 # in Makefile: keeping related stuff altogether.
557 my %dependencies;
558
559 # Holds the factored actions.  Tied to %DEPENDENCIES, i.e., filled
560 # only when keys exists in %DEPENDENCIES.
561 my %actions;
562
563 # A list of files deleted by `maintainer-clean'.
564 my @maintainer_clean_files;
565
566 # Keys in this hash table are object files or other files in
567 # subdirectories which need to be removed.  This only holds files
568 # which are created by compilations.  The value in the hash indicates
569 # when the file should be removed.
570 my %compile_clean_files;
571
572 # Value of `$(SOURCES)', used by tags.am.
573 my @sources;
574 # Sources which go in the distribution.
575 my @dist_sources;
576
577 # This hash maps object file names onto their corresponding source
578 # file names.  This is used to ensure that each object is created
579 # by a single source file.
580 my %object_map;
581
582 # This keeps track of the directories for which we've already
583 # created `.dirstamp' code.
584 my %directory_map;
585
586 # All .P files.
587 my %dep_files;
588
589 # Strictness levels.
590 my $strictness;
591 my $strictness_name;
592
593 # Options from AUTOMAKE_OPTIONS.
594 my %options;
595
596 # Whether or not dependencies are handled.  Can be further changed
597 # in handle_options.
598 my $use_dependencies;
599
600 # Per Makefile.am.
601 my $local_maint_charset;
602
603 # All yacc and lex source filenames for this directory.  Use
604 # filenames instead of raw count so that multiple instances are
605 # counted correctly (eg one yacc file can appear in multiple
606 # programs without harm).
607 my %yacc_sources;
608 my %lex_sources;
609
610 # This is a list of all targets to run during "make dist".
611 my @dist_targets;
612
613 # Keys in this hash are the basenames of files which must depend
614 # on ansi2knr.
615 my %de_ansi_files;
616
617 # This maps the source extension of a suffix rule to its
618 # corresponding output extension.
619 my %suffix_rules;
620
621 # This is the name of the redirect `all' target to use.
622 my $all_target;
623
624 # This keeps track of which extensions we've seen (that we care
625 # about).
626 my %extension_seen;
627
628 # This is random scratch space for the language finish functions.
629 # Don't randomly overwrite it; examine other uses of keys first.
630 my %language_scratch;
631
632 # We keep track of which objects need special (per-executable)
633 # handling on a per-language basis.
634 my %lang_specific_files;
635
636 # This is set when `handle_dist' has finished.  Once this happens,
637 # we should no longer push on dist_common.
638 my $handle_dist_run;
639
640 # True if we need `LINK' defined.  This is a hack.
641 my $need_link;
642
643 # This is the list of such variables to output.
644 # FIXME: Might be useless actually.
645 my @var_list;
646
647 # Was get_object_extension run?
648 # FIXME: This is a hack. a better switch should be found.
649 my $get_object_extension_was_run;
650
651
652 ## --------------------------------- ##
653 ## Forward subroutine declarations.  ##
654 ## --------------------------------- ##
655 sub register_language (%);
656 sub file_contents_internal ($$%);
657
658
659 # &initialize_per_input ()
660 # ------------------------
661 # (Re)-Initialize per-Makefile.am variables.
662 sub initialize_per_input ()
663 {
664     $am_file_name = '';
665     $am_relative_dir = '';
666
667     $in_file_name = '';
668     $relative_dir = '';
669
670     $output_rules = '';
671     $output_vars = '';
672     $output_trailer = '';
673     $output_all = '';
674     $output_header = '';
675
676     @suffixes = ();
677
678     %var_value = ();
679     %var_line = ();
680     %var_comment = ();
681     %var_type = ();
682     %var_is_am = ();
683
684     %content_seen = ();
685
686     %targets = ();
687
688     %target_conditional = ();
689
690     @cond_stack = ();
691
692     @include_stack = ();
693
694     $relative_dir = '';
695
696     $am_relative_dir = '';
697
698     %dist_dirs = ();
699
700     @all = ();
701     @check = ();
702     @check_tests = ();
703
704     %dependencies =
705       (
706        # Texinfoing.
707        'dvi'      => [],
708        'dvi-am'   => [],
709        'info'     => [],
710        'info-am'  => [],
711
712        # Installing/uninstalling.
713        'install-data-am'      => [],
714        'install-exec-am'      => [],
715        'uninstall-am'         => [],
716
717        'install-man'          => [],
718        'uninstall-man'        => [],
719
720        'install-info'         => [],
721        'install-info-am'      => [],
722        'uninstall-info'       => [],
723
724        'installcheck-am'      => [],
725
726        # Cleaning.
727        'clean-am'             => [],
728        'mostlyclean-am'       => [],
729        'maintainer-clean-am'  => [],
730        'distclean-am'         => [],
731        'clean'                => [],
732        'mostlyclean'          => [],
733        'maintainer-clean'     => [],
734        'distclean'            => [],
735
736        # Tarballing.
737        'dist-all'             => [],
738
739        # Phoning.
740        '.PHONY'               => []
741       );
742     %actions = ();
743
744     @maintainer_clean_files = ();
745
746     @sources = ();
747     @dist_sources = ();
748
749     %object_map = ();
750
751     %directory_map = ();
752
753     %dep_files = ();
754
755     $strictness = $default_strictness;
756     $strictness_name = $default_strictness_name;
757
758     %options = ();
759
760     $use_dependencies = $cmdline_use_dependencies;
761
762     $local_maint_charset = $maint_charset;
763
764     %yacc_sources = ();
765     %lex_sources = ();
766
767     @dist_targets = ();
768
769     %de_ansi_files = ();
770
771     %suffix_rules = ();
772
773     $all_target = '';
774
775     %extension_seen = ();
776
777     %language_scratch = ();
778
779     %lang_specific_files = ();
780
781     $handle_dist_run = 0;
782
783     $need_link = 0;
784
785     @var_list = ();
786
787     $get_object_extension_was_run = 0;
788
789     %compile_clean_files = ();
790 }
791
792
793 ################################################################
794
795 # Initialize our list of languages that are internally supported.
796
797 # C.
798 register_language ('name' => 'c',
799                    'Name' => 'C',
800                    'config_vars' => ['CC'],
801                    'ansi' => 1,
802                    'autodep' => '',
803                    'flags' => 'CFLAGS',
804                    'compiler' => 'COMPILE',
805                    'compile' => '$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)',
806                    'lder' => 'CCLD',
807                    'ld' => '$(CC)',
808                    'linker' => 'LINK',
809                    'link' => '$(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@',
810                    'compile_flag' => '-c',
811                    'extensions' => ['c'],
812                    '_finish' => \&lang_c_finish);
813
814 # C++.
815 register_language ('name' => 'cxx',
816                    'Name' => 'C++',
817                    'config_vars' => ['CXX'],
818                    'linker' => 'CXXLINK',
819                    'link' => '$(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@',
820                    'autodep' => 'CXX',
821                    'flags' => 'CXXFLAGS',
822                    'compile' => '$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)',
823                    'compiler' => 'CXXCOMPILE',
824                    'compile_flag' => '-c',
825                    'output_flag' => '-o',
826                    'lder' => 'CXXLD',
827                    'ld' => '$(CXX)',
828                    'pure' => 1,
829                    'extensions' => ['c++', 'cc', 'cpp', 'cxx', 'C']);
830
831 # Objective C.
832 register_language ('name' => 'objc',
833                    'Name' => 'Objective C',
834                    'config_vars' => ['OBJC'],
835                    'linker' => 'OBJCLINK',,
836                    'link' => '$(OBJCLD) $(AM_OBJCFLAGS) $(OBJCFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@',
837                    'autodep' => 'OBJC',
838                    'flags' => 'OBJCFLAGS',
839                    'compile' => '$(OBJC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_OBJCFLAGS) $(OBJCFLAGS)',
840                    'compiler' => 'OBJCCOMPILE',
841                    'compile_flag' => '-c',
842                    'output_flag' => '-o',
843                    'lder' => 'OBJCLD',
844                    'ld' => '$(OBJC)',
845                    'pure' => 1,
846                    'extensions' => ['m']);
847
848 # Headers.
849 register_language ('name' => 'header',
850                    'Name' => 'Header',
851                    'extensions' => ['h', 'H', 'hxx', 'h++', 'hh', 'hpp', 'inc'],
852                    # Nothing to do.
853                    '_finish' => sub { });
854
855 # For now, yacc and lex can't be handled on a per-exe basis.
856
857 # Yacc (C & C++).
858 register_language ('name' => 'yacc',
859                    'Name' => 'Yacc',
860                    'ansi' => 1,
861                    'config_vars' => ['YACC'],
862                    'derived_autodep' => 'yes',
863                    'extensions' => ['y'],
864                    '_finish' => \&lang_yacc_finish);
865 register_language ('name' => 'yaccxx',
866                    'Name' => 'Yacc (C++)',
867                    'config_vars' => ['YACC'],
868                    'linker' => 'CXXLINK',
869                    'derived_autodep' => 'yes',
870                    'extensions' => ['y++', 'yy', 'yxx', 'ypp'],
871                    '_finish' => \&lang_yacc_finish);
872
873 # Lex (C & C++).
874 register_language ('name' => 'lex',
875                    'Name' => 'Lex',
876                    'ansi' => 1,
877                    'config_vars' => ['LEX'],
878                    'derived_autodep' => 'yes',
879                    'extensions' => ['l'],
880                    '_finish' => \&lang_lex_finish);
881 register_language ('name' => 'lexxx',
882                    'Name' => 'Lex (C++)',
883                    'config_vars' => ['LEX'],
884                    'linker' => 'CXXLINK',
885                    'derived_autodep' => 'yes',
886                    'extensions' => ['l++', 'll', 'lxx', 'lpp'],
887                    '_finish' => \&lang_lex_finish);
888
889 # Assembler.
890 register_language ('name' => 'asm',
891                    'Name' => 'Assembler',
892                    'config_vars' => ['AS', 'ASFLAGS'],
893
894                    'flags' => 'ASFLAGS',
895                    # Users can set AM_ASFLAGS to includes DEFS, INCLUDES,
896                    # or anything else required.  They can also set AS.
897                    'compile' => '$(AS) $(AM_ASFLAGS) $(ASFLAGS)',
898                    'compiler' => 'ASCOMPILE',
899                    'compile_flag' => '-c',
900                    'extensions' => ['s', 'S'],
901
902                    # With assembly we still use the C linker.
903                    '_finish' => \&lang_c_finish);
904
905 # Fortran 77
906 register_language ('name' => 'f77',
907                    'Name' => 'Fortran 77',
908                    'linker' => 'F77LINK',
909                    'link' => '$(F77LD) $(AM_FFLAGS) $(FFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@',
910                    'flags' => 'FFLAGS',
911                    'compile' => '$(F77) $(AM_FFLAGS) $(FFLAGS)',
912                    'compiler' => 'F77COMPILE',
913                    'compile_flag' => '-c',
914                    'output_flag' => '-o',
915                    'lder' => 'F77LD',
916                    'ld' => '$(F77)',
917                    'pure' => 1,
918                    'extensions' => ['f', 'for', 'f90']);
919
920 # Preprocessed Fortran 77
921 #
922 # The current support for preprocessing Fortran 77 just involves
923 # passing `$(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS)
924 # $(CPPFLAGS)' as additional flags to the Fortran 77 compiler, since
925 # this is how GNU Make does it; see the `GNU Make Manual, Edition 0.51
926 # for `make' Version 3.76 Beta' (specifically, from info file
927 # `(make)Catalogue of Rules').
928 #
929 # A better approach would be to write an Autoconf test
930 # (i.e. AC_PROG_FPP) for a Fortran 77 preprocessor, because not all
931 # Fortran 77 compilers know how to do preprocessing.  The Autoconf
932 # macro AC_PROG_FPP should test the Fortran 77 compiler first for
933 # preprocessing capabilities, and then fall back on cpp (if cpp were
934 # available).
935 register_language ('name' => 'ppf77',
936                    'Name' => 'Preprocessed Fortran 77',
937                    'config_vars' => ['F77'],
938                    'linker' => 'F77LINK',
939                    'link' => '$(F77LD) $(AM_FFLAGS) $(FFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@',
940                    'lder' => 'F77LD',
941                    'ld' => '$(F77)',
942                    'flags' => 'FFLAGS',
943                    'compiler' => 'PPF77COMPILE',
944                    'compile' => '$(F77) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_FFLAGS) $(FFLAGS)',
945                    'compile_flag' => '-c',
946                    'output_flag' => '-o',
947                    'pure' => 1,
948                    'extensions' => ['F']);
949
950 # Ratfor.
951 register_language ('name' => 'ratfor',
952                    'Name' => 'Ratfor',
953                    'config_vars' => ['F77'],
954                    'linker' => 'F77LINK',
955                    'link' => '$(F77LD) $(AM_FFLAGS) $(FFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@',
956                    'lder' => 'F77LD',
957                    'ld' => '$(F77)',
958                    'flags' => 'RFLAGS',
959                    # FIXME also FFLAGS.
960                    'compile' => '$(F77) $(AM_FFLAGS) $(FFLAGS) $(AM_RFLAGS) $(RFLAGS)',
961                    'compiler' => 'RCOMPILE',
962                    'compile_flag' => '-c',
963                    'output_flag' => '-o',
964                    'pure' => 1,
965                    'extensions' => ['r']);
966
967 # Java via gcj.
968 register_language ('name' => 'java',
969                    'Name' => 'Java',
970                    'config_vars' => ['GCJ'],
971                    'linker' => 'GCJLINK',
972                    'link' => '$(GCJLD) $(AM_GCJFLAGS) $(GCJFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@',
973                    'autodep' => 'GCJ',
974                    'flags' => 'GCJFLAGS',
975                    'compile' => '$(GCJ) $(AM_GCJFLAGS) $(GCJFLAGS)',
976                    'compiler' => 'GCJCOMPILE',
977                    'compile_flag' => '-c',
978                    'output_flag' => '-o',
979                    'lder' => 'GCJLD',
980                    'ld' => '$(GCJ)',
981                    'pure' => 1,
982                    'extensions' => ['java', 'class', 'zip', 'jar']);
983
984 ################################################################
985
986 # Parse command line.
987 &parse_arguments;
988
989 # Do configure.ac scan only once.
990 &scan_autoconf_files;
991
992 die "$me: no `Makefile.am' found or specified\n"
993     if ! @input_files;
994
995 # Now do all the work on each file.
996 # This guy must be local otherwise it's private to the loop.
997 use vars '$am_file';
998 local $am_file;
999 foreach $am_file (@input_files)
1000 {
1001     if (! -f ($am_file . '.am'))
1002     {
1003         &am_error ("`" . $am_file . ".am' does not exist");
1004     }
1005     else
1006     {
1007         &generate_makefile ($output_files{$am_file}, $am_file);
1008     }
1009 }
1010
1011 exit $exit_status;
1012
1013 # FIXME: This should be `my'ed next to its subs.
1014 use vars '%require_file_found';
1015
1016 ################################################################
1017
1018 # prog_error (@PRINT-ME)
1019 # ----------------------
1020 # Signal a programming error, display PRINT-ME, and exit 1.
1021 sub prog_error (@)
1022 {
1023     print STDERR "$me: programming error: @_\n";
1024     exit 1;
1025 }
1026
1027
1028 # @RES
1029 # uniq (@LIST)
1030 # ------------
1031 # Return LIST with no duplicates.
1032 sub uniq (@)
1033 {
1034    my @res = ();
1035    my %seen = ();
1036    foreach my $item (@_)
1037      {
1038        if (! defined $seen{$item})
1039          {
1040            $seen{$item} = 1;
1041            push (@res, $item);
1042          }
1043      }
1044    return @res;
1045 }
1046
1047 # subst ($TEXT)
1048 # -------------
1049 # Return a configure-style substitution using the indicated text.
1050 # We do this to avoid having the substitutions directly in automake.in;
1051 # when we do that they are sometimes removed and this causes confusion
1052 # and bugs.
1053 sub subst ($)
1054 {
1055     my ($text) = @_;
1056     return '@' . $text . '@';
1057 }
1058
1059 ################################################################
1060
1061
1062 # $BACKPATH
1063 # &backname ($REL-DIR)
1064 # --------------------
1065 # If I `cd $REL-DIR', then to come back, I should `cd $BACKPATH'.
1066 # For instance `src/foo' => `../..'.
1067 # Works with non strictly increasing paths, i.e., `src/../lib' => `..'.
1068 sub backname ($)
1069 {
1070     my ($file) = @_;
1071     my @res;
1072     foreach (split (/\//, $file))
1073     {
1074         next if $_ eq '.' || $_ eq '';
1075         if ($_ eq '..')
1076         {
1077             pop @res;
1078         }
1079         else
1080         {
1081             push (@res, '..');
1082         }
1083     }
1084     return join ('/', @res) || '.';
1085 }
1086
1087 ################################################################
1088
1089 # Parse command line.
1090 sub parse_arguments ()
1091 {
1092     # Start off as gnu.
1093     &set_strictness ('gnu');
1094
1095     use Getopt::Long;
1096     Getopt::Long::config ("bundling");
1097     Getopt::Long::GetOptions
1098       (
1099        'version'        => \&version,
1100        'help'           => \&usage,
1101        'libdir:s'       => \$libdir,
1102        'gnu'            => sub { &set_strictness ('gnu'); },
1103        'gnits'          => sub { &set_strictness ('gnits'); },
1104        'cygnus'         => \$cygnus_mode,
1105        'foreign'        => sub { &set_strictness ('foreign'); },
1106        'include-deps'   => sub { $cmdline_use_dependencies = 1; },
1107        'i|ignore-deps'  => sub { $cmdline_use_dependencies = 0; },
1108        'no-force'       => sub { $force_generation = 0; },
1109        'f|force-missing'=> \$force_missing,
1110        'o|output-dir:s' => \$output_directory,
1111        'a|add-missing'  => \$add_missing,
1112        'c|copy'         => \$copy_missing,
1113        'v|verbose'      => \$verbose,
1114        'Werror'         => sub { $SIG{"__WARN__"} = sub { die $_[0] } },
1115        'Wno-error'      => sub { $SIG{"__WARN__"} = 'DEFAULT' }
1116       )
1117         or exit 1;
1118
1119     foreach my $arg (@ARGV)
1120     {
1121       # Handle $local:$input syntax.  Note that we only examine the
1122       # first ":" file to see if it is automake input; the rest are
1123       # just taken verbatim.  We still keep all the files around for
1124       # dependency checking, however.
1125       my ($local, $input, @rest) = split (/:/, $arg);
1126       if (! $input)
1127         {
1128           $input = $local;
1129         }
1130       else
1131         {
1132           # Strip .in; later on .am is tacked on.  That is how the
1133           # automake input file is found.  Maybe not the best way, but
1134           # it is easy to explain.
1135           $input =~ s/\.in$//
1136             or die "$me: invalid input file name `$arg'\n.";
1137         }
1138       push (@input_files, $input);
1139       $output_files{$input} = join (':', ($local, @rest));
1140     }
1141
1142     # Take global strictness from whatever we currently have set.
1143     $default_strictness = $strictness;
1144     $default_strictness_name = $strictness_name;
1145 }
1146
1147 ################################################################
1148
1149 # Generate a Makefile.in given the name of the corresponding Makefile and
1150 # the name of the file output by config.status.
1151 sub generate_makefile
1152 {
1153     my ($output, $makefile) = @_;
1154
1155     # Reset all the Makefile.am related variables.
1156     &initialize_per_input;
1157
1158     # Name of input file ("Makefile.am") and output file
1159     # ("Makefile.in").  These have no directory components.
1160     $am_file_name = basename ($makefile) . '.am';
1161     $in_file_name = basename ($makefile) . '.in';
1162
1163     # $OUTPUT is encoded.  If it contains a ":" then the first element
1164     # is the real output file, and all remaining elements are input
1165     # files.  We don't scan or otherwise deal with these input file,
1166     # other than to mark them as dependencies.  See
1167     # &scan_autoconf_files for details.
1168     my (@secondary_inputs);
1169     ($output, @secondary_inputs) = split (/:/, $output);
1170
1171     $relative_dir = dirname ($output);
1172     $am_relative_dir = dirname ($makefile);
1173
1174     &read_main_am_file ($makefile . '.am');
1175     if (&handle_options)
1176     {
1177         # Fatal error.  Just return, so we can continue with next file.
1178         return;
1179     }
1180
1181     # There are a few install-related variables that you should not define.
1182     foreach my $var ('PRE_INSTALL', 'POST_INSTALL', 'NORMAL_INSTALL')
1183     {
1184         if (&variable_defined ($var) && !$var_is_am{$var})
1185         {
1186             &am_line_error ($var, "`$var' should not be defined");
1187         }
1188     }
1189
1190     # At the toplevel directory, we might need config.guess, config.sub
1191     # or libtool scripts (ltconfig and ltmain.sh).
1192     if ($relative_dir eq '.')
1193     {
1194         # libtool requires some files.
1195         &require_conf_file_with_conf_line ($libtool_line, $FOREIGN,
1196                                            @libtoolize_files)
1197             if $seen_libtool;
1198
1199         # AC_CANONICAL_HOST and AC_CANONICAL_SYSTEM need config.guess and
1200         # config.sub.
1201         &require_config_file ($FOREIGN, 'config.guess', 'config.sub')
1202             if $seen_canonical;
1203     }
1204
1205     # We still need Makefile.in here, because sometimes the `dist'
1206     # target doesn't re-run automake.
1207     if ($am_relative_dir eq $relative_dir)
1208     {
1209         # Only distribute the files if they are in the same subdir as
1210         # the generated makefile.
1211         &push_dist_common ($in_file_name, $am_file_name);
1212     }
1213
1214     push (@sources, '$(SOURCES)')
1215         if &variable_defined ('SOURCES');
1216
1217     # If OBJEXT/EXEEXT were not set in configure.in, do it, it
1218     # simplifies our task, and anyway starting with Autoconf 2.50, it
1219     # will always be defined, and this code will be dead.
1220     $output_vars .= "EXEEXT =\n"
1221       unless $seen_exeext;
1222     $output_vars .= "OBJEXT = o\n"
1223       unless $seen_objext;
1224
1225     # Must do this after reading .am file.  See read_main_am_file to
1226     # understand weird tricks we play there with variables.
1227     &define_variable ('subdir', $relative_dir);
1228
1229     # Check first, because we might modify some state.
1230     &check_cygnus;
1231     &check_gnu_standards;
1232     &check_gnits_standards;
1233
1234     &handle_configure ($output, $makefile, @secondary_inputs);
1235     &handle_gettext;
1236     &handle_libraries;
1237     &handle_ltlibraries;
1238     &handle_programs;
1239     &handle_scripts;
1240
1241     # This must be run after all the sources are scanned.
1242     &handle_languages;
1243     &handle_compile;
1244
1245     # Re-init SOURCES.  FIXME: other code shouldn't depend on this
1246     # (but currently does).
1247     macro_define ('SOURCES', 1, '', 'TRUE',
1248                      join (' ', @sources), 'internal');
1249     &define_pretty_variable ('DIST_SOURCES', '', @dist_sources);
1250
1251     &handle_multilib;
1252     &handle_texinfo;
1253     &handle_emacs_lisp;
1254     &handle_python;
1255     &handle_java;
1256     &handle_man_pages;
1257     &handle_data;
1258     &handle_headers;
1259     &handle_subdirs;
1260     &handle_tags;
1261     &handle_minor_options;
1262     &handle_tests;
1263
1264     # This must come after most other rules.
1265     &handle_dist ($makefile);
1266
1267     &handle_footer;
1268     &do_check_merge_target;
1269     &handle_all ($output);
1270
1271     # FIXME: Gross!
1272     if (&variable_defined('lib_LTLIBRARIES') &&
1273         &variable_defined('bin_PROGRAMS'))
1274     {
1275         $output_rules .= "install-binPROGRAMS: install-libLTLIBRARIES\n\n";
1276     }
1277
1278     &handle_installdirs;
1279     &handle_clean;
1280     &handle_factored_dependencies;
1281
1282     &check_typos;
1283
1284     if (! -d ($output_directory . '/' . $am_relative_dir))
1285     {
1286         mkdir ($output_directory . '/' . $am_relative_dir, 0755);
1287     }
1288
1289     my ($out_file) = $output_directory . '/' . $makefile . ".in";
1290     if (! $force_generation && -e $out_file)
1291     {
1292         my ($am_time) = (stat ($makefile . '.am'))[9];
1293         my ($in_time) = (stat ($out_file))[9];
1294         # FIXME: should cache these times.
1295         my ($conf_time) = (stat ($configure_ac))[9];
1296         # FIXME: how to do unsigned comparison?
1297         if ($am_time < $in_time || $am_time < $conf_time)
1298         {
1299             # No need to update.
1300             return;
1301         }
1302         if (-f 'aclocal.m4')
1303         {
1304             my ($acl_time) = (stat _)[9];
1305             return if ($am_time < $acl_time);
1306         }
1307     }
1308
1309     my $gm_file = new IO::File "> $out_file";
1310     if (! $gm_file)
1311     {
1312         warn "$me: ${am_file}.in: cannot write: $!\n";
1313         $exit_status = 1;
1314         return;
1315     }
1316     print "$me: creating ", $makefile, ".in\n" if $verbose;
1317
1318     # In case we're running under MSWindows, don't write with CRLF
1319     # (as it causes problems for the dependency-file extraction in
1320     # AM_OUTPUT_DEPENDENCY_COMMANDS).
1321     binmode $gm_file;
1322
1323     print $gm_file $output_vars;
1324     # We make sure that `all:' is the first target.
1325     print $gm_file $output_all;
1326     print $gm_file $output_header;
1327     print $gm_file $output_rules;
1328     print $gm_file $output_trailer;
1329
1330     if (! $gm_file->close)
1331       {
1332         warn "$me: $am_file.in: cannot close: $!\n";
1333         $exit_status = 1;
1334         return;
1335       }
1336 }
1337
1338 ################################################################
1339
1340 # Handle AUTOMAKE_OPTIONS variable.  Return 1 on error, 0 otherwise.
1341 sub handle_options
1342 {
1343     if (&variable_defined ('AUTOMAKE_OPTIONS'))
1344     {
1345         foreach (&variable_value_as_list ('AUTOMAKE_OPTIONS', ''))
1346         {
1347             $options{$_} = 1;
1348             if ($_ eq 'gnits' || $_ eq 'gnu' || $_ eq 'foreign')
1349             {
1350                 &set_strictness ($_);
1351             }
1352             elsif ($_ eq 'cygnus')
1353             {
1354                 $cygnus_mode = 1;
1355             }
1356             elsif (/ansi2knr/)
1357             {
1358                 # An option like "../lib/ansi2knr" is allowed.  With
1359                 # no path prefix, we assume the required programs are
1360                 # in this directory.  We save the actual option for
1361                 # later.
1362                 $options{'ansi2knr'} = $_;
1363             }
1364             elsif ($_ eq 'no-installman' || $_ eq 'no-installinfo'
1365                    || $_ eq 'dist-shar' || $_ eq 'dist-zip'
1366                    || $_ eq 'dist-tarZ' || $_ eq 'dist-bzip2'
1367                    || $_ eq 'dejagnu' || $_ eq 'no-texinfo.tex'
1368                    || $_ eq 'readme-alpha' || $_ eq 'check-news'
1369                    || $_ eq 'subdir-objects' || $_ eq 'nostdinc')
1370             {
1371                 # Explicitly recognize these.
1372             }
1373             elsif ($_ eq 'no-dependencies')
1374             {
1375                 $use_dependencies = 0;
1376             }
1377             elsif (/(\d+)\.(\d+)([a-z]?)(-[A-Za-z0-9]+)?/)
1378             {
1379                 # Got a version number.
1380
1381                 my ($rmajor, $rminor, $ralpha, $rfork) = ($1, $2, $3, $4);
1382
1383                 &prog_error ("version is incorrect: $VERSION")
1384                   if $VERSION !~ /(\d+)\.(\d+)([a-z]?)(-[A-Za-z0-9]+)?/;
1385
1386                 my ($tmajor, $tminor, $talpha, $tfork) = ($1, $2, $3, $4);
1387
1388                 $rfork ||= '';
1389                 $tfork ||= '';
1390
1391                 # 2.0 is better than 1.0.
1392                 # 1.2 is better than 1.1.
1393                 # 1.2a is better than 1.2.
1394                 # If we require 3.4n-foo then we require something
1395                 # >= 3.4n, with the `foo' fork identifier.
1396                 if ($rmajor > $tmajor
1397                     || ($rmajor == $tmajor && $rminor > $tminor)
1398                     || ($rminor == $tminor && $rminor == $tminor
1399                         && $ralpha gt $talpha)
1400                     || ($rfork ne '' && $rfork ne $tfork))
1401                   {
1402                     &am_line_error ('AUTOMAKE_OPTIONS',
1403                                     "require version $_, but have $VERSION");
1404                     return 1;
1405                   }
1406             }
1407             else
1408             {
1409                 &am_line_error ('AUTOMAKE_OPTIONS',
1410                                 "option `" . $_ . "\' not recognized");
1411             }
1412         }
1413     }
1414
1415     if ($strictness == $GNITS)
1416     {
1417         $options{'readme-alpha'} = 1;
1418         $options{'check-news'} = 1;
1419     }
1420
1421     return 0;
1422 }
1423
1424
1425 # get_object_extension ($OUT)
1426 # ---------------------------
1427 # Return object extension.  Just once, put some code into the output.
1428 # OUT is the name of the output file
1429 sub get_object_extension
1430 {
1431     my ($out) = @_;
1432
1433     # Maybe require libtool library object files.
1434     my $extension = '.$(OBJEXT)';
1435     $extension = '.lo' if ($out =~ /\.la$/);
1436
1437     # Check for automatic de-ANSI-fication.
1438     $extension = '$U' . $extension
1439       if defined $options{'ansi2knr'};
1440
1441     $get_object_extension_was_run = 1;
1442
1443     return $extension;
1444 }
1445
1446
1447 # Call finish function for each language that was used.
1448 sub handle_languages
1449 {
1450     if ($use_dependencies)
1451     {
1452         # Include auto-dep code.  Don't include it if DEP_FILES would
1453         # be empty.
1454         if (&saw_sources_p (0) && keys %dep_files)
1455         {
1456             # Set location of depcomp.
1457             &define_variable ('depcomp', "\$(SHELL) $config_aux_dir/depcomp");
1458
1459             &require_config_file ($FOREIGN, 'depcomp');
1460
1461             my @deplist = sort keys %dep_files;
1462
1463             # We define this as a conditional variable because BSD
1464             # make can't handle backslashes for continuing comments on
1465             # the following line.
1466             &define_pretty_variable ('DEP_FILES', 'AMDEP_TRUE', @deplist);
1467
1468             # Generate each `include' individually.  Irix 6 make will
1469             # not properly include several files resulting from a
1470             # variable expansion; generating many separate includes
1471             # seems safest.
1472             $output_rules .= "\n";
1473             foreach my $iter (@deplist)
1474             {
1475                 $output_rules .= (subst ('AMDEP_TRUE')
1476                                   . subst ('_am_include')
1477                                   . ' '
1478                                   . subst ('_am_quote')
1479                                   . $iter
1480                                   . subst ('_am_quote')
1481                                   . "\n");
1482             }
1483
1484             $output_rules .= &file_contents ('depend');
1485         }
1486     }
1487     else
1488     {
1489         &define_variable ('depcomp', '');
1490     }
1491
1492     my %done;
1493
1494     # Is the c linker needed?
1495     my $needs_c = 0;
1496     foreach my $ext (sort keys %extension_seen)
1497       {
1498         my $lang = $languages{$extension_map{$ext}};
1499
1500         # Get information on $LANG.
1501         my $pfx = $lang->autodep;
1502         my $fpfx = ($pfx eq '') ? 'CC' : $pfx;
1503
1504         my $AMDEP = (($use_dependencies && $lang->autodep ne 'no')
1505                      ? 'AMDEP' : 'FALSE');
1506
1507         my %transform = ('EXT'     => $ext,
1508                          'PFX'     => $pfx,
1509                          'FPFX'    => $fpfx,
1510                          'LIBTOOL' => $seen_libtool,
1511                          'AMDEP'   => $AMDEP,
1512                          '-c'      => $lang->compile_flag || '');
1513
1514         # Generate the appropriate rules for this extension.
1515         if ($use_dependencies && $lang->autodep ne 'no'
1516             || defined $lang->compile)
1517           {
1518             # Some C compilers don't support -c -o.  Use it only if really
1519             # needed.
1520             my $output_flag = $lang->output_flag || '';
1521             $output_flag = '-o'
1522               if (! $output_flag
1523                   && $lang->flags eq 'CFLAGS'
1524                   && defined $options{'subdir-objects'});
1525
1526             $output_rules .=
1527               file_contents ('depend2',
1528                              %transform,
1529                              'GENERIC'   => 1,
1530
1531                              'BASE'      => '$*',
1532                              'SOURCE'    => '$<',
1533                              'OBJ'       => '$@',
1534                              'OBJOBJ'    => '$@',
1535                              'LTOBJ'     => '$@',
1536
1537                              'COMPILE'   => '$(' . $lang->compiler . ')',
1538                              'LTCOMPILE' => '$(LT' . $lang->compiler . ')',
1539                              '-o'        => $output_flag);
1540           }
1541
1542         # Now include code for each specially handled object with this
1543         # language.
1544         my %seen_files = ();
1545         foreach my $file (@{$lang_specific_files{$lang->name}})
1546           {
1547             my ($derived, $source, $obj) = split (' ', $file);
1548
1549             # We might see a given object twice, for instance if it is
1550             # used under different conditions.
1551             next if defined $seen_files{$obj};
1552             $seen_files{$obj} = 1;
1553
1554             my $flags = $lang->flags || '';
1555             my $val = "${derived}_${flags}";
1556
1557             (my $obj_compile = $lang->compile) =~ s/\(AM_$flags/\($val/;
1558             my $obj_ltcompile = '$(LIBTOOL) --mode=compile ' . $obj_compile;
1559
1560             # We _need_ `-o' for per object rules.
1561             my $output_flag = $lang->output_flag || '-o';
1562
1563             # Generate a transform which will turn suffix targets in
1564             # depend2.am into real targets for the particular objects we
1565             # are building.
1566             $output_rules .=
1567               file_contents ('depend2',
1568                              (%transform,
1569                               'GENERIC'   => 0,
1570
1571                               'BASE'      => $obj,
1572                               'SOURCE'    => $source,
1573                               'OBJ'       => "$obj.o",
1574                               'OBJOBJ'    => "$obj.obj",
1575                               'LTOBJ'     => "$obj.lo",
1576
1577                               'COMPILE'   => $obj_compile,
1578                               'LTCOMPILE' => $obj_ltcompile,
1579                               '-o'        => $output_flag));
1580             }
1581
1582         # The rest of the loop is done once per language.
1583         next if defined $done{$lang};
1584         $done{$lang} = 1;
1585
1586         # Load the language dependent Makefile chunks.
1587         my %lang = map { uc ($_) => 0 } keys %languages;
1588         $lang{uc ($lang->name)} = 1;
1589         $output_rules .= file_contents ('lang-compile', %transform, %lang);
1590
1591         # If the source to a program consists entirely of code from a
1592         # `pure' language, for instance C++ for Fortran 77, then we
1593         # don't need the C compiler code.  However if we run into
1594         # something unusual then we do generate the C code.  There are
1595         # probably corner cases here that do not work properly.
1596         # People linking Java code to Fortran code deserve pain.
1597         $needs_c ||= ! $lang->pure;
1598
1599         define_compiler_variable ($lang)
1600           if ($lang->compile);
1601
1602         define_linker_variable ($lang)
1603           if ($lang->link);
1604
1605         foreach my $var (@{$lang->config_vars})
1606           {
1607             am_error ($lang->Name
1608                       . " source seen but `$var' not defined in"
1609                       . " `$configure_ac'")
1610               if !exists $configure_vars{$var};
1611           }
1612
1613         # The compiler's flag must be a configure variable.
1614         define_configure_variable ($lang->flags)
1615           if (defined $lang->flags);
1616
1617         # Call the finisher.
1618         $lang->finish;
1619       }
1620
1621     # If the project is entirely C++ or entirely Fortran 77 (i.e., 1
1622     # suffix rule was learned), don't bother with the C stuff.  But if
1623     # anything else creeps in, then use it.
1624     $needs_c = 1
1625       if $need_link || scalar keys %suffix_rules > 1;
1626
1627     if ($needs_c)
1628       {
1629         if (! defined $done{$languages{'c'}})
1630           {
1631             &define_configure_variable ($languages{'c'}->flags);
1632             &define_compiler_variable ($languages{'c'});
1633           }
1634         define_linker_variable ($languages{'c'});
1635       }
1636 }
1637
1638 # Output a rule to build from a YACC source.  The output from YACC is
1639 # compiled with C or C++, depending on the extension of the YACC file.
1640 sub output_yacc_build_rule
1641 {
1642     my ($yacc_suffix, $use_ylwrap) = @_;
1643
1644     (my $c_suffix = $yacc_suffix) =~ tr/y/c/;
1645
1646     # Generate rule for c/c++.
1647     $output_rules .= &file_contents ('yacc',
1648                                      ('YLWRAP'      => $use_ylwrap,
1649                                       'YACC_SUFFIX' => $yacc_suffix,
1650                                       'C_SUFFIX'    => $c_suffix));
1651 }
1652
1653 sub output_lex_build_rule
1654 {
1655     my ($lex_suffix, $use_ylwrap) = @_;
1656
1657     (my $c_suffix = $lex_suffix) =~ tr/l/c/;
1658
1659     $output_rules .= &file_contents ('lex',
1660                                      ('YLWRAP'     => $use_ylwrap,
1661                                       'LEX_SUFFIX' => $lex_suffix,
1662                                       'C_SUFFIX'   => $c_suffix));
1663 }
1664
1665 # Check to make sure a source defined in LIBOBJS is not explicitly
1666 # mentioned.  This is a separate function (as opposed to being inlined
1667 # in handle_source_transform) because it isn't always appropriate to
1668 # do this check.
1669 sub check_libobjs_sources
1670 {
1671     my ($one_file, $unxformed) = @_;
1672
1673     foreach my $prefix ('', 'EXTRA_', 'dist_', 'nodist_',
1674                         'dist_EXTRA_', 'nodist_EXTRA_')
1675     {
1676         my @files;
1677         if (&variable_defined ($prefix . $one_file . '_SOURCES'))
1678         {
1679             @files = &variable_value_as_list (($prefix
1680                                                . $one_file . '_SOURCES'),
1681                                               'all');
1682         }
1683         elsif ($prefix eq '')
1684         {
1685             @files = ($unxformed . '.c');
1686         }
1687         else
1688         {
1689             next;
1690         }
1691
1692         foreach my $file (@files)
1693         {
1694             if (defined $libsources{$file})
1695             {
1696                 &am_line_error ($prefix . $one_file . '_SOURCES',
1697                                 "automatically discovered file `$file' should not be explicitly mentioned");
1698             }
1699         }
1700     }
1701 }
1702
1703
1704 # ($LINKER, @OBJECTS)
1705 # handle_single_transform_list ($VAR, $DERIVED, $OBJ, @FILES)
1706 # -----------------------------------------------------------
1707 # Does much of the actual work for handle_source_transform.
1708 # Arguments are:
1709 #   $DERIVED is the name of resulting executable or library
1710 #   $OBJ is the object extension (e.g., `$U.lo')
1711 #   @FILES is the list of source files to transform
1712 # Result is a list
1713 #   $LINKER is name of linker to use (empty string for default linker)
1714 #   @OBJECTS are names of objects
1715 sub handle_single_transform_list ($$$@)
1716 {
1717     my ($var, $derived, $obj, @files) = @_;
1718     my @result = ();
1719     my $nonansi_obj = $obj;
1720     $nonansi_obj =~ s/\$U//g;
1721     my %linkers_used = ();
1722
1723     # Turn sources into objects.
1724     foreach (@files)
1725     {
1726         # Configure substitutions in _SOURCES variables are errors.
1727         if (/^\@.*\@$/)
1728         {
1729             &am_line_error ($var, "$var includes configure substitution `$_'");
1730             next;
1731         }
1732
1733         # If the source file is in a subdirectory then the `.o' is
1734         # put into the current directory.
1735
1736         # Split file name into base and extension.
1737         next if ! /^(?:(.*)\/)?([^\/]*)\.(.*)$/;
1738         my $full = $_;
1739         my $directory = $1 || '';
1740         my $base = $2;
1741         my $extension = $3;
1742
1743         # We must generate a rule for the object if it requires its own flags.
1744         my $renamed = 0;
1745         my ($linker, $object);
1746
1747         $extension = &derive_suffix ($extension);
1748         my $lang = $languages{$extension_map{$extension}};
1749         if ($lang)
1750         {
1751             &saw_extension ($extension);
1752             # Found the language, so see what it says.
1753             my $subr = 'lang_' . $lang->name . '_rewrite';
1754             # Note: computed subr call.
1755             my $r = & $subr ($directory, $base, $extension);
1756             # Skip this entry if we were asked not to process it.
1757             next if $r == $LANG_IGNORE;
1758
1759             # Now extract linker and other info.
1760             $linker = $lang->linker;
1761
1762             my $this_obj_ext = $lang->ansi ? $obj : $nonansi_obj;
1763             $object = $base . $this_obj_ext;
1764
1765             if (defined $lang->flags
1766                 && &variable_defined ($derived . '_' . $lang->flags))
1767             {
1768                 # We have a per-executable flag in effect for this
1769                 # object.  In this case we rewrite the object's
1770                 # name to ensure it is unique.  We also require
1771                 # the `compile' program to deal with compilers
1772                 # where `-c -o' does not work.
1773
1774                 # We choose the name `DERIVED_OBJECT' to ensure
1775                 # (1) uniqueness, and (2) continuity between
1776                 # invocations.  However, this will result in a
1777                 # name that is too long for losing systems, in
1778                 # some situations.  So we provide _SHORTNAME to
1779                 # override.
1780
1781                 my $dname = $derived;
1782                 if (&variable_defined ($derived . '_SHORTNAME'))
1783                 {
1784                     # FIXME: should use the same conditional as
1785                     # the _SOURCES variable.  But this is really
1786                     # silly overkill -- nobody should have
1787                     # conditional shortnames.
1788                     $dname = &variable_value ($derived . '_SHORTNAME');
1789                 }
1790                 $object = $dname . '-' . $object;
1791
1792                 &require_file ($FOREIGN, 'compile')
1793                     if $lang->name eq 'c';
1794
1795                 &prog_error ("$lang->name flags defined without compiler")
1796                     if ! defined $lang->compile;
1797
1798                 $renamed = 1;
1799             }
1800
1801             # If rewrite said it was ok, put the object into a
1802             # subdir.
1803             if ($r == $LANG_SUBDIR && $directory ne '')
1804             {
1805                 $object = $directory . '/' . $object;
1806             }
1807
1808             # If doing dependency tracking, then we can't print
1809             # the rule.  If we have a subdir object, we need to
1810             # generate an explicit rule.  Actually, in any case
1811             # where the object is not in `.' we need a special
1812             # rule.  The per-object rules in this case are
1813             # generated later, by handle_languages.
1814             if ($renamed || $directory ne '')
1815             {
1816                 my $obj_sans_ext = substr ($object, 0,
1817                                            - length ($this_obj_ext));
1818                 push (@{$lang_specific_files{$lang->name}},
1819                       "$derived $full $obj_sans_ext");
1820             }
1821         }
1822         elsif ($extension eq 'o')
1823         {
1824             # This is probably the result of a direct suffix rule.
1825             # In this case we just accept the rewrite.  FIXME:
1826             # this fails if we want libtool objects.
1827             $object = $base . '.' . $extension;
1828             $linker = '';
1829         }
1830         else
1831         {
1832             # No error message here.  Used to have one, but it was
1833             # very unpopular.
1834             next;
1835         }
1836
1837         $linkers_used{$linker} = 1;
1838
1839         push (@result, $object);
1840
1841         if (defined $object_map{$object})
1842         {
1843             if ($object_map{$object} ne $full)
1844             {
1845                 &am_error ("object `$object' created by `$full' and `$object_map{$object}'");
1846             }
1847         }
1848         else
1849         {
1850             my @dep_list = ();
1851             $object_map{$object} = $full;
1852
1853             # If file is in subdirectory, we need explicit
1854             # dependency.
1855             if ($directory ne '' || $renamed)
1856             {
1857                 push (@dep_list, $full);
1858             }
1859
1860             # If resulting object is in subdir, we need to make
1861             # sure the subdir exists at build time.
1862             if ($object =~ /\//)
1863             {
1864                 # FIXME: check that $DIRECTORY is somewhere in the
1865                 # project
1866
1867                 # We don't allow `..' in object file names for
1868                 # *any* source, not just Java.  For Java it just
1869                 # doesn't make sense, but in general it is
1870                 # a problem because we can't pick a good name for
1871                 # the .deps entry.
1872                 if ($object =~ /(\/|^)\.\.\//)
1873                 {
1874                     &am_error ("`$full' contains `..' component but should not");
1875                 }
1876
1877                 # Make sure object is removed by `make mostlyclean'.
1878                 $compile_clean_files{$object} = $MOSTLY_CLEAN;
1879
1880                 push (@dep_list, $directory . '/.dirstamp');
1881
1882                 # If we're generating dependencies, we also want
1883                 # to make sure that the appropriate subdir of the
1884                 # .deps directory is created.
1885                 if ($use_dependencies)
1886                 {
1887                     push (@dep_list, '.deps/' . $directory . '/.dirstamp');
1888                 }
1889
1890                 if (! defined $directory_map{$directory})
1891                 {
1892                     $directory_map{$directory} = 1;
1893
1894                     # Directory must be removed by `make distclean'.
1895                     $compile_clean_files{$directory . "/.dirstamp"} =
1896                         $DIST_CLEAN;
1897                     $output_rules .= ($directory . "/.dirstamp:\n"
1898                                       . "\t\@\$(mkinstalldirs) $directory\n"
1899                                       . "\t\@: > $directory/.dirstamp\n");
1900                     if ($use_dependencies)
1901                     {
1902                         $output_rules .= ('.deps/' . $directory
1903                                           . "/.dirstamp:\n"
1904                                           . "\t\@\$(mkinstalldirs) .deps/$directory\n"
1905                                           . "\t\@: > .deps/$directory/.dirstamp\n");
1906                     }
1907                 }
1908             }
1909
1910             &pretty_print_rule ($object . ':', "\t", @dep_list)
1911                 if scalar @dep_list > 0;
1912         }
1913
1914         # Transform .o or $o file into .P file (for automatic
1915         # dependency code).
1916         if ($lang
1917             && ($lang->autodep ne 'no'
1918                 || $lang->derived_autodep eq 'yes'))
1919         {
1920             my $depfile = $object;
1921             $depfile =~ s/\.([^.]*)$/.P$1/;
1922             $depfile =~ s/\$\(OBJEXT\)$/o/;
1923             $dep_files{'$(DEPDIR)/' . $depfile} = 1;
1924         }
1925     }
1926
1927     return (&resolve_linker (%linkers_used), @result);
1928 }
1929
1930
1931
1932 # Handle SOURCE->OBJECT transform for one program or library.
1933 # Arguments are:
1934 #   canonical (transformed) name of object to build
1935 #   actual name of object to build
1936 #   object extension (ie either `.o' or `$o'.
1937 # Return result is name of linker variable that must be used.
1938 # Empty return means just use `LINK'.
1939 sub handle_source_transform
1940 {
1941     # one_file is canonical name.  unxformed is given name.  obj is
1942     # object extension.
1943     my ($one_file, $unxformed, $obj) = @_;
1944
1945     my ($linker) = '';
1946
1947     if (&variable_defined ($one_file . "_OBJECTS"))
1948     {
1949         &am_line_error ($one_file . '_OBJECTS',
1950                         $one_file . '_OBJECTS', 'should not be defined');
1951         # No point in continuing.
1952         return;
1953     }
1954
1955     my %used_pfx = ();
1956     foreach my $prefix ('', 'EXTRA_', 'dist_', 'nodist_',
1957                         'dist_EXTRA_', 'nodist_EXTRA_')
1958     {
1959         my $var = $prefix . $one_file . "_SOURCES";
1960         next
1961           if !variable_defined ($var);
1962
1963         # We are going to define _OBJECTS variables using the prefix.
1964         # Then we glom them all together.  So we can't use the null
1965         # prefix here as we need it later.
1966         my $xpfx = ($prefix eq '') ? 'am_' : $prefix;
1967
1968         # Keep track of which prefixes we saw.
1969         $used_pfx{$xpfx} = 1
1970           unless $prefix =~ /EXTRA_/;
1971
1972         push (@sources, '$(' . $prefix . $one_file . "_SOURCES)");
1973         push (@dist_sources, '$(' . $prefix . $one_file . "_SOURCES)")
1974           unless $prefix =~ /^nodist_/;
1975         foreach my $cond (variable_conditions ($var))
1976           {
1977             my @files = &variable_value_as_list ($var, $cond);
1978             my ($temp, @result) =
1979               &handle_single_transform_list ($var, $one_file, $obj,
1980                                              @files);
1981             # If there are no files to compile, don't require a linker (yet).
1982             $linker ||= $temp
1983               if @files;
1984
1985             # Define _OBJECTS conditionally.
1986             &define_pretty_variable ($xpfx . $one_file . '_OBJECTS',
1987                                      $cond, @result)
1988               unless $prefix =~ /EXTRA_/;
1989           }
1990     }
1991
1992     my @keys = sort keys %used_pfx;
1993     if (scalar @keys == 0)
1994     {
1995         &define_variable ($one_file . "_SOURCES", $unxformed . ".c");
1996         push (@sources, $unxformed . '.c');
1997         push (@dist_sources, $unxformed . '.c');
1998
1999         my ($temp, @result) =
2000           &handle_single_transform_list ($one_file . '_SOURCES',
2001                                          $one_file, $obj,
2002                                          "$unxformed.c");
2003         $linker = $temp if $linker eq '';
2004         &define_pretty_variable ($one_file . "_OBJECTS", '', @result)
2005     }
2006     else
2007     {
2008         grep ($_ = '$(' . $_ . $one_file . '_OBJECTS)', @keys);
2009         &define_pretty_variable ($one_file . '_OBJECTS', '', @keys);
2010     }
2011
2012     # If we want to use `LINK' we must make sure it is defined.
2013     if ($linker eq '')
2014     {
2015         $need_link = 1;
2016     }
2017
2018     return $linker;
2019 }
2020
2021
2022 # handle_lib_objects ($XNAME, $VAR)
2023 # ---------------------------------
2024 # Special-case @ALLOCA@ and @LIBOBJS@ in _LDADD or _LIBADD variables.
2025 # Also, generate _DEPENDENCIES variable if appropriate.
2026 # Arguments are:
2027 #   transformed name of object being built, or empty string if no object
2028 #   name of _LDADD/_LIBADD-type variable to examine
2029 # Returns 1 if LIBOBJS seen, 0 otherwise.
2030 sub handle_lib_objects
2031 {
2032     my ($xname, $var) = @_;
2033
2034     &prog_error ("handle_lib_objects: $var undefined")
2035         if ! &variable_defined ($var);
2036
2037     my $ret = 0;
2038     foreach my $cond (&variable_conditions ($var))
2039       {
2040         if (&handle_lib_objects_cond ($xname, $var, $cond))
2041           {
2042             $ret = 1;
2043           }
2044       }
2045     return $ret;
2046 }
2047
2048 # Subroutine of handle_lib_objects: handle a particular condition.
2049 sub handle_lib_objects_cond
2050 {
2051     my ($xname, $var, $cond) = @_;
2052
2053     # We recognize certain things that are commonly put in LIBADD or
2054     # LDADD.
2055     my @dep_list = ();
2056
2057     my $seen_libobjs = 0;
2058     my $flagvar = 0;
2059
2060     foreach my $lsearch (&variable_value_as_list ($var, $cond))
2061     {
2062         # Skip -lfoo and -Ldir; these are explicitly allowed.
2063         next if $lsearch =~ /^-[lL]/;
2064         if (! $flagvar && $lsearch =~ /^-/)
2065         {
2066             if ($var =~ /^(.*)LDADD$/)
2067             {
2068                 # Skip -dlopen and -dlpreopen; these are explicitly allowed.
2069                 next if $lsearch =~ /^-dl(pre)?open$/;
2070                 &am_line_error ($var, "linker flags such as `$lsearch' belong in `${1}LDFLAGS");
2071             }
2072             else
2073             {
2074                 # Only get this error once.
2075                 $flagvar = 1;
2076                 &am_line_error ($var, "linker flags such as `$lsearch' belong in `${1}LDFLAGS");
2077             }
2078         }
2079
2080         # Assume we have a file of some sort, and push it onto the
2081         # dependency list.  Autoconf substitutions are not pushed;
2082         # rarely is a new dependency substituted into (eg) foo_LDADD
2083         # -- but "bad things (eg -lX11) are routinely substituted.
2084         # Note that LIBOBJS and ALLOCA are exceptions to this rule,
2085         # and handled specially below.
2086         push (@dep_list, $lsearch)
2087             unless $lsearch =~ /^\@.*\@$/;
2088
2089         # Automatically handle @LIBOBJS@ and @ALLOCA@.  Basically this
2090         # means adding entries to dep_files.
2091         if ($lsearch =~ /^\@(LT)?LIBOBJS\@$/)
2092         {
2093             my $myobjext = ($1 ? 'l' : '') . 'o';
2094
2095             push (@dep_list, $lsearch);
2096             $seen_libobjs = 1;
2097             if (! keys %libsources
2098                 && ! &variable_defined ($1 . 'LIBOBJS'))
2099             {
2100                 &am_line_error ($var, "\@$1" . "LIBOBJS\@ seen but never set in `$configure_ac'");
2101             }
2102
2103             foreach my $iter (keys %libsources)
2104             {
2105                 if ($iter =~ /\.([cly])$/)
2106                 {
2107                     &saw_extension ($1);
2108                     &saw_extension ('c');
2109                 }
2110
2111                 if ($iter =~ /\.h$/)
2112                 {
2113                     &require_file_with_line ($var, $FOREIGN, $iter);
2114                 }
2115                 elsif ($iter ne 'alloca.c')
2116                 {
2117                     my $rewrite = $iter;
2118                     $rewrite =~ s/\.c$/.P$myobjext/;
2119                     $dep_files{'$(DEPDIR)/' . $rewrite} = 1;
2120                     ($rewrite = $iter) =~ s/(\W)/\\$1/g;
2121                     $rewrite = "^" . $rewrite . "\$";
2122                     # Only require the file if it is not a built source.
2123                     if (! &variable_defined ('BUILT_SOURCES')
2124                         || ! grep (/$rewrite/,
2125                                    &variable_value_as_list ('BUILT_SOURCES',
2126                                                             'all')))
2127                     {
2128                         &require_file_with_line ($var, $FOREIGN, $iter);
2129                     }
2130                 }
2131             }
2132         }
2133         elsif ($lsearch =~ /^\@(LT)?ALLOCA\@$/)
2134         {
2135             my $myobjext = ($1 ? 'l' : '') . 'o';
2136
2137             push (@dep_list, $lsearch);
2138             &am_line_error ($var,
2139                             "\@$1" . "ALLOCA\@ seen but `AC_FUNC_ALLOCA' not in `$configure_ac'")
2140                 if ! defined $libsources{'alloca.c'};
2141             $dep_files{'$(DEPDIR)/alloca.P' . $myobjext} = 1;
2142             &require_file_with_line ($var, $FOREIGN, 'alloca.c');
2143             &saw_extension ('c');
2144         }
2145     }
2146
2147     if ($xname ne '' && ! &variable_defined ($xname . '_DEPENDENCIES', $cond))
2148     {
2149         &define_pretty_variable ($xname . '_DEPENDENCIES', $cond, @dep_list);
2150     }
2151
2152     return $seen_libobjs;
2153 }
2154
2155 # Canonicalize the input parameter
2156 sub canonicalize
2157 {
2158     my ($string) = @_;
2159     $string =~ tr/A-Za-z0-9_\@/_/c;
2160     return $string;
2161 }
2162
2163 # Canonicalize a name, and check to make sure the non-canonical name
2164 # is never used.  Returns canonical name.  Arguments are name and a
2165 # list of suffixes to check for.
2166 sub check_canonical_spelling
2167 {
2168     my ($name, @suffixes) = @_;
2169
2170     my $xname = &canonicalize ($name);
2171     if ($xname ne $name)
2172     {
2173         foreach my $xt (@suffixes)
2174         {
2175             &am_line_error ("$name$xt",
2176                             "invalid variable `$name$xt'; "
2177                             . "should be `$xname$xt'")
2178                 if &variable_defined ("$name$xt");
2179         }
2180     }
2181
2182     return $xname;
2183 }
2184
2185
2186 # handle_compile ()
2187 # -----------------
2188 # Set up the compile suite.
2189 sub handle_compile ()
2190 {
2191     return
2192       unless $get_object_extension_was_run;
2193
2194     # Boilerplate.
2195     my $default_includes = '';
2196     if (! defined $options{'nostdinc'})
2197       {
2198         $default_includes = ' -I. -I$(srcdir)';
2199
2200         if (&variable_defined ('CONFIG_HEADER'))
2201           {
2202             foreach my $hdr (split (' ', &variable_value ('CONFIG_HEADER')))
2203               {
2204                 $default_includes .= ' -I' . dirname ($hdr);
2205               }
2206           }
2207       }
2208
2209     my (@mostly_rms, @dist_rms);
2210     foreach my $item (sort keys %compile_clean_files)
2211     {
2212         if ($compile_clean_files{$item} == $MOSTLY_CLEAN)
2213         {
2214             push (@mostly_rms, "\t-rm -f $item");
2215         }
2216         elsif ($compile_clean_files{$item} == $DIST_CLEAN)
2217         {
2218             push (@dist_rms, "\t-rm -f $item");
2219         }
2220         else
2221         {
2222             &prog_error ("invalid entry in \%compile_clean_files");
2223         }
2224     }
2225
2226     my ($coms, $vars, $rules) =
2227       &file_contents_internal (1, "$libdir/am/compile.am",
2228                                ('DEFAULT_INCLUDES' => $default_includes,
2229                                 'MOSTLYRMS' => join ("\n", @mostly_rms),
2230                                 'DISTRMS' => join ("\n", @dist_rms)));
2231     $output_vars .= $vars;
2232     $output_rules .= "$coms$rules";
2233
2234     if ($seen_libtool)
2235       {
2236         # Output the libtool compilation rules.
2237         $output_rules .= &file_contents ('libtool');
2238       }
2239
2240     # Check for automatic de-ANSI-fication.
2241     if (defined $options{'ansi2knr'})
2242       {
2243         if (! $am_c_prototypes)
2244           {
2245             &am_line_error ('AUTOMAKE_OPTIONS',
2246                             "option `ansi2knr' in use but `AM_C_PROTOTYPES' not in `$configure_ac'");
2247             &keyed_aclocal_warning ('AM_C_PROTOTYPES');
2248             # Only give this error once.
2249             $am_c_prototypes = 1;
2250           }
2251
2252         # topdir is where ansi2knr should be.
2253         if ($options{'ansi2knr'} eq 'ansi2knr')
2254           {
2255             # Only require ansi2knr files if they should appear in
2256             # this directory.
2257             &require_file_with_line ('AUTOMAKE_OPTIONS', $FOREIGN,
2258                                      'ansi2knr.c', 'ansi2knr.1');
2259
2260             # ansi2knr needs to be built before subdirs, so unshift it.
2261             unshift (@all, '$(ANSI2KNR)');
2262           }
2263
2264         my $ansi2knr_dir = '';
2265         $ansi2knr_dir = dirname ($options{'ansi2knr'})
2266           if $options{'ansi2knr'} ne 'ansi2knr';
2267
2268         $output_rules .= &file_contents ('ansi2knr',
2269                                          ('ANSI2KNR-DIR' => $ansi2knr_dir));
2270
2271     }
2272 }
2273
2274
2275
2276 # handle_programs ()
2277 # ------------------
2278 # Handle C programs.
2279 sub handle_programs
2280 {
2281     my @proglist = &am_install_var ('progs', 'PROGRAMS',
2282                                     'bin', 'sbin', 'libexec', 'pkglib',
2283                                     'noinst', 'check');
2284     return if ! @proglist;
2285
2286     my $seen_libobjs = 0;
2287     foreach my $one_file (@proglist)
2288     {
2289         my $obj = &get_object_extension ($one_file);
2290
2291         # Canonicalize names and check for misspellings.
2292         my $xname = &check_canonical_spelling ($one_file, '_LDADD', '_LDFLAGS',
2293                                                '_SOURCES', '_OBJECTS',
2294                                                '_DEPENDENCIES');
2295
2296         my $linker = &handle_source_transform ($xname, $one_file, $obj);
2297
2298         my $xt = '';
2299         if (&variable_defined ($xname . "_LDADD"))
2300         {
2301             if (&handle_lib_objects ($xname, $xname . '_LDADD'))
2302             {
2303                 $seen_libobjs = 1;
2304             }
2305             $xt = '_LDADD';
2306         }
2307         else
2308         {
2309             # User didn't define prog_LDADD override.  So do it.
2310             &define_variable ($xname . '_LDADD', '$(LDADD)');
2311
2312             # This does a bit too much work.  But we need it to
2313             # generate _DEPENDENCIES when appropriate.
2314             if (&variable_defined ('LDADD'))
2315             {
2316                 if (&handle_lib_objects ($xname, 'LDADD'))
2317                 {
2318                     $seen_libobjs = 1;
2319                 }
2320             }
2321             elsif (! &variable_defined ($xname . '_DEPENDENCIES'))
2322             {
2323                 &define_variable ($xname . '_DEPENDENCIES', '');
2324             }
2325             $xt = '_SOURCES'
2326         }
2327
2328         if (&variable_defined ($xname . '_LIBADD'))
2329         {
2330             &am_line_error ($xname . '_LIBADD',
2331                             "use `" . $xname . "_LDADD', not `"
2332                             . $xname . "_LIBADD'");
2333         }
2334
2335         if (! &variable_defined ($xname . '_LDFLAGS'))
2336         {
2337             # Define the prog_LDFLAGS variable.
2338             &define_variable ($xname . '_LDFLAGS', '');
2339         }
2340
2341         # Determine program to use for link.
2342         my $xlink;
2343         if (&variable_defined ($xname . '_LINK'))
2344         {
2345             $xlink = $xname . '_LINK';
2346         }
2347         else
2348         {
2349             $xlink = $linker ? $linker : 'LINK';
2350         }
2351
2352         $output_rules .= &file_contents ('program',
2353                                          ('PROGRAM'  => $one_file,
2354                                           'XPROGRAM' => $xname,
2355                                           'XLINK'    => $xlink));
2356     }
2357
2358     if (&variable_defined ('LDADD') && &handle_lib_objects ('', 'LDADD'))
2359     {
2360         $seen_libobjs = 1;
2361     }
2362
2363     if ($seen_libobjs)
2364     {
2365         foreach my $one_file (@proglist)
2366         {
2367             my $xname = &canonicalize ($one_file);
2368
2369             if (&variable_defined ($xname . '_LDADD'))
2370             {
2371                 &check_libobjs_sources ($xname, $xname . '_LDADD');
2372             }
2373             elsif (&variable_defined ('LDADD'))
2374             {
2375                 &check_libobjs_sources ($xname, 'LDADD');
2376             }
2377         }
2378     }
2379 }
2380
2381
2382 # handle_libraries ()
2383 # -------------------
2384 # Handle libraries.
2385 sub handle_libraries
2386 {
2387     my @liblist = &am_install_var ('libs', 'LIBRARIES',
2388                                    'lib', 'pkglib', 'noinst', 'check');
2389     return if ! @liblist;
2390
2391     my %valid = &am_primary_prefixes ('LIBRARIES', 0, 'lib', 'pkglib',
2392                                       'noinst', 'check');
2393     if (! defined $configure_vars{'RANLIB'})
2394     {
2395         foreach my $key (keys %valid)
2396         {
2397             if (&variable_defined ($key . '_LIBRARIES'))
2398             {
2399                 &am_line_error ($key . '_LIBRARIES', "library used but `RANLIB' not defined in `$configure_ac'");
2400                 # Only get this error once.  If this is ever printed,
2401                 # we have a bug.
2402                 $configure_vars{'RANLIB'} = 'BUG';
2403                 last;
2404             }
2405         }
2406     }
2407
2408     my $seen_libobjs = 0;
2409     foreach my $onelib (@liblist)
2410     {
2411         # Check that the library fits the standard naming convention.
2412         if ($onelib !~ /^lib.*\.a$/)
2413         {
2414             # FIXME should put line number here.  That means mapping
2415             # from library name back to variable name.
2416             &am_error ("`$onelib' is not a standard library name");
2417         }
2418
2419         my $obj = &get_object_extension ($onelib);
2420
2421         # Canonicalize names and check for misspellings.
2422         my $xlib = &check_canonical_spelling ($onelib, '_LIBADD', '_SOURCES',
2423                                               '_OBJECTS', '_DEPENDENCIES',
2424                                               '_AR');
2425
2426         if (! &variable_defined ($xlib . '_AR'))
2427         {
2428             &define_variable ($xlib . '_AR', '$(AR) cru');
2429         }
2430
2431         if (&variable_defined ($xlib . '_LIBADD'))
2432         {
2433             if (&handle_lib_objects ($xlib, $xlib . '_LIBADD'))
2434             {
2435                 $seen_libobjs = 1;
2436             }
2437         }
2438         else
2439         {
2440             # Generate support for conditional object inclusion in
2441             # libraries.
2442             &define_variable ($xlib . "_LIBADD", '');
2443         }
2444
2445         if (&variable_defined ($xlib . '_LDADD'))
2446         {
2447             &am_line_error ($xlib . '_LDADD',
2448                             "use `" . $xlib . "_LIBADD', not `"
2449                             . $xlib . "_LDADD'");
2450         }
2451
2452         # Make sure we at look at this.
2453         &examine_variable ($xlib . '_DEPENDENCIES');
2454
2455         &handle_source_transform ($xlib, $onelib, $obj);
2456
2457         $output_rules .= &file_contents ('library',
2458                                          ('LIBRARY'  => $onelib,
2459                                           'XLIBRARY' => $xlib));
2460     }
2461
2462     if ($seen_libobjs)
2463     {
2464         foreach my $onelib (@liblist)
2465         {
2466             my $xlib = &canonicalize ($onelib);
2467             if (&variable_defined ($xlib . '_LIBADD'))
2468             {
2469                 &check_libobjs_sources ($xlib, $xlib . '_LIBADD');
2470             }
2471         }
2472     }
2473 }
2474
2475
2476 # handle_ltlibraries ()
2477 # ---------------------
2478 # Handle shared libraries.
2479 sub handle_ltlibraries
2480 {
2481     my @liblist = &am_install_var ('ltlib', 'LTLIBRARIES',
2482                                    'noinst', 'lib', 'pkglib', 'check');
2483     return if ! @liblist;
2484
2485     my %instdirs;
2486     my %valid = &am_primary_prefixes ('LTLIBRARIES', 0, 'lib', 'pkglib',
2487                                       'noinst', 'check');
2488
2489     foreach my $key (keys %valid)
2490     {
2491         if (&variable_defined ($key . '_LTLIBRARIES'))
2492         {
2493             if (!$seen_libtool)
2494             {
2495                 &am_line_error ($key . '_LTLIBRARIES', "library used but `LIBTOOL' not defined in `$configure_ac'");
2496                 # Only get this error once.  If this is ever printed,
2497                 # we have a bug.
2498                 $configure_vars{'LIBTOOL'} = 'BUG';
2499                 $seen_libtool = 1;
2500             }
2501
2502             # Get the installation directory of each library.
2503             for (&variable_value_as_list ($key . '_LTLIBRARIES', 'all'))
2504             {
2505                 if ($instdirs{$_})
2506                 {
2507                     &am_error ("`$_' is already going to be installed in `$instdirs{$_}'");
2508                 }
2509                 else
2510                 {
2511                     $instdirs{$_} = $key;
2512                 }
2513             }
2514         }
2515     }
2516
2517     my $seen_libobjs = 0;
2518     foreach my $onelib (@liblist)
2519     {
2520         my $obj = &get_object_extension ($onelib);
2521
2522         # Canonicalize names and check for misspellings.
2523         my $xlib = &check_canonical_spelling ($onelib, '_LIBADD', '_LDFLAGS',
2524                                               '_SOURCES', '_OBJECTS',
2525                                               '_DEPENDENCIES');
2526
2527         if (! &variable_defined ($xlib . '_LDFLAGS'))
2528         {
2529             # Define the lib_LDFLAGS variable.
2530             &define_variable ($xlib . '_LDFLAGS', '');
2531         }
2532
2533         # Check that the library fits the standard naming convention.
2534         my $libname_rx = "^lib.*\.la";
2535         if ((&variable_defined ($xlib . '_LDFLAGS')
2536              && grep (/-module/, &variable_value_as_list ($xlib . '_LDFLAGS',
2537                                                           'all')))
2538             || (&variable_defined ('LDFLAGS')
2539                 && grep (/-module/, &variable_value_as_list ('LDFLAGS',
2540                                                              'all'))))
2541         {
2542                 # Relax name checking for libtool modules.
2543                 $libname_rx = "\.la";
2544         }
2545         if ($onelib !~ /$libname_rx$/)
2546         {
2547             # FIXME this should only be a warning for foreign packages
2548             # FIXME should put line number here.  That means mapping
2549             # from library name back to variable name.
2550             &am_error ("`$onelib' is not a standard libtool library name");
2551         }
2552
2553         if (&variable_defined ($xlib . '_LIBADD'))
2554         {
2555             if (&handle_lib_objects ($xlib, $xlib . '_LIBADD'))
2556             {
2557                 $seen_libobjs = 1;
2558             }
2559         }
2560         else
2561         {
2562             # Generate support for conditional object inclusion in
2563             # libraries.
2564             &define_variable ($xlib . "_LIBADD", '');
2565         }
2566
2567         if (&variable_defined ($xlib . '_LDADD'))
2568         {
2569             &am_line_error ($xlib . '_LDADD',
2570                             "use `" . $xlib . "_LIBADD', not `"
2571                             . $xlib . "_LDADD'");
2572         }
2573
2574         # Make sure we at look at this.
2575         &examine_variable ($xlib . '_DEPENDENCIES');
2576
2577         my $linker = &handle_source_transform ($xlib, $onelib, $obj);
2578
2579         # Determine program to use for link.
2580         my $xlink;
2581         if (&variable_defined ($xlib . '_LINK'))
2582         {
2583             $xlink = $xlib . '_LINK';
2584         }
2585         else
2586         {
2587             $xlink = $linker ? $linker : 'LINK';
2588         }
2589
2590         my $rpath;
2591         if ($instdirs{$onelib} eq 'EXTRA'
2592             || $instdirs{$onelib} eq 'noinst'
2593             || $instdirs{$onelib} eq 'check')
2594         {
2595             # It's an EXTRA_ library, so we can't specify -rpath,
2596             # because we don't know where the library will end up.
2597             # The user probably knows, but generally speaking automake
2598             # doesn't -- and in fact configure could decide
2599             # dynamically between two different locations.
2600             $rpath = '';
2601         }
2602         else
2603         {
2604             $rpath = ('-rpath $(' . $instdirs{$onelib} . 'dir)');
2605         }
2606
2607         $output_rules .= &file_contents ('ltlibrary',
2608                                          ('LTLIBRARY'  => $onelib,
2609                                           'XLTLIBRARY' => $xlib,
2610                                           'RPATH'      => $rpath,
2611                                           'XLINK'      => $xlink));
2612     }
2613
2614     if ($seen_libobjs)
2615     {
2616         foreach my $onelib (@liblist)
2617         {
2618             my $xlib = &canonicalize ($onelib);
2619             if (&variable_defined ($xlib . '_LIBADD'))
2620             {
2621                 &check_libobjs_sources ($xlib, $xlib . '_LIBADD');
2622             }
2623         }
2624     }
2625 }
2626
2627 # See if any _SOURCES variable were misspelled.  Also, make sure that
2628 # EXTRA_ variables don't contain configure substitutions.
2629 sub check_typos
2630 {
2631     foreach my $varname (keys %var_value)
2632     {
2633         foreach my $primary ('_SOURCES', '_LIBADD', '_LDADD', '_LDFLAGS',
2634                              '_DEPENDENCIES')
2635         {
2636             if ($varname =~ /$primary$/ && ! $content_seen{$varname})
2637             {
2638                 &am_line_error ($varname,
2639                                 "invalid unused variable name: `$varname'");
2640             }
2641         }
2642     }
2643 }
2644
2645 # Handle scripts.
2646 sub handle_scripts
2647 {
2648     # NOTE we no longer automatically clean SCRIPTS, because it is
2649     # useful to sometimes distribute scripts verbatim.  This happens
2650     # eg in Automake itself.
2651     &am_install_var ('-candist', 'scripts', 'SCRIPTS',
2652                      'bin', 'sbin', 'libexec', 'pkgdata',
2653                      'noinst', 'check');
2654
2655     my $scripts_installed = 0;
2656     # Set $scripts_installed if appropriate.  Make sure we only find
2657     # scripts which are actually installed -- this is why we can't
2658     # simply use the return value of am_install_var.
2659     my %valid = &am_primary_prefixes ('SCRIPTS', 1, 'bin', 'sbin',
2660                                       'libexec', 'pkgdata',
2661                                       'noinst', 'check');
2662     foreach my $key (keys %valid)
2663     {
2664         if ($key ne 'noinst'
2665             && $key ne 'check'
2666             && &variable_defined ($key . '_SCRIPTS'))
2667         {
2668             $scripts_installed = 1;
2669             # push (@check_tests, 'check-' . $key . 'SCRIPTS');
2670         }
2671     }
2672 }
2673
2674
2675 # ($OUTFILE, $VFILE, @CLEAN_FILES)
2676 # &scan_texinfo_file ($FILENAME)
2677 # ------------------------------
2678 # $OUTFILE is the name of the info file produced by $FILENAME.
2679 # $VFILE is the name of the version.texi file used (empty if none).
2680 # @CLEAN_FILES is the list of by products (indexes etc.)
2681 sub scan_texinfo_file
2682 {
2683     my ($filename) = @_;
2684
2685     # These are always created, no matter whether indexes are used or not.
2686     my @clean_suffixes = ('aux', 'dvi', 'log', 'ps', 'toc',
2687                           # grep new.*index texinfo.tex
2688                           'cp', 'fn', 'ky', 'vr', 'tp', 'pg');
2689
2690     # There are predefined indexes which don't follow the regular rules.
2691     my %predefined_index =
2692       (
2693        # cindex => *.cps
2694        'c' => 'cps', 'f' => 'fns', 'k' => 'kys',
2695        'v' => 'vrs', 't' => 'tps', 'p' => 'pgs'
2696       );
2697
2698     # There are commands which include a hidden index command.
2699     my %hidden_index =
2700       (
2701        # deffn => *.fns.
2702        'fn' => 'fns',     'un' => 'fns',
2703        'typefn' => 'fns', 'typefun' => 'fns',
2704        'mac' => 'fns', 'spec' => 'fns',
2705        'op' => 'fns',  'typeop' => 'fns',
2706        'method' => 'fns', 'typemethod' => 'fns',
2707
2708        'vr' => 'vrs', 'var' => 'vrs',
2709        'typevr' => 'vrs', 'typevar' => 'vrs',
2710        'opt' => 'vrs',
2711        'cv' => 'vrs',
2712        'ivar' => 'vrs', 'typeivar' => 'vrs',
2713
2714        'tp' => 'tps'
2715       );
2716
2717     # Indexes stored into another one.  In this case, the *.??s file
2718     # is not created.
2719     my @syncodeindexes = ();
2720
2721     my $texi = new IO::File ("< $filename");
2722     if (! $texi)
2723       {
2724         &am_error ("couldn't open `$filename': $!");
2725         return '';
2726     }
2727     print "$me: reading $filename\n" if $verbose;
2728
2729     my ($outfile, $vfile);
2730     while ($_ = $texi->getline)
2731     {
2732       if (/^\@setfilename +(\S+)/)
2733       {
2734         $outfile = $1;
2735         if ($outfile =~ /\.(.+)$/ && $1 ne 'info')
2736           {
2737             &am_file_error ($filename, "$.: ",
2738                             "output `$outfile' has unrecognized extension");
2739             return;
2740           }
2741       }
2742       # A "version.texi" file is actually any file whose name
2743       # matches "vers*.texi".
2744       elsif (/^\@include\s+(vers[^.]*\.texi)\s*$/)
2745       {
2746         $vfile = $1;
2747       }
2748
2749       # Try to find what are the indexes which are used.
2750
2751       # Creating a new category of index.
2752       elsif (/^\@def(code)?index (\w+)/)
2753       {
2754         push @clean_suffixes, $2;
2755       }
2756
2757       # Storing in a predefined index.
2758       elsif (/^\@([cfkvtp])index /)
2759       {
2760         push @clean_suffixes, $predefined_index{$1};
2761       }
2762       elsif (/^\@def(\w+) /)
2763       {
2764         push @clean_suffixes, $hidden_index{$1}
2765           if defined $hidden_index{$1};
2766       }
2767
2768       # Merging an index into an another.
2769       elsif (/^\@syn(code)?index (\w+) \w+/)
2770       {
2771         push @syncodeindexes, "$2s";
2772       }
2773
2774     }
2775     $texi->close;
2776
2777     if ($outfile eq '')
2778       {
2779         &am_error ("`$filename' missing \@setfilename");
2780         return;
2781       }
2782
2783     my $infobase = basename ($filename);
2784     $infobase =~ s/\.te?xi(nfo)?$//;
2785     # FIXME: I don't understand why, but I can't use "$infobase.$_" => 1.
2786     my %clean_files = map { "$infobase" . ".$_" => 1 } @clean_suffixes;
2787     grep { delete $clean_files{"$infobase.$_"} } @syncodeindexes;
2788     return ($outfile, $vfile, (sort keys %clean_files));
2789 }
2790
2791
2792 # ($DO-SOMETHING, $TEXICLEANS)
2793 # handle_texinfo_helper ()
2794 # -----------------
2795 # Handle all Texinfo source; helper for handle_texinfo
2796 sub handle_texinfo_helper
2797 {
2798     &am_line_error ('TEXINFOS',
2799                     "`TEXINFOS' is an anachronism; use `info_TEXINFOS'")
2800         if &variable_defined ('TEXINFOS');
2801     return (0, '') if (! &variable_defined ('info_TEXINFOS')
2802                        && ! &variable_defined ('html_TEXINFOS'));
2803
2804     if (&variable_defined ('html_TEXINFOS'))
2805     {
2806         &am_line_error ('html_TEXINFOS',
2807                         "HTML generation not yet supported");
2808         return (0, '');
2809     }
2810
2811     my @texis = &variable_value_as_list ('info_TEXINFOS', 'all');
2812
2813     my (@info_deps_list, @dvis_list, @texi_deps);
2814     my %versions;
2815     my $done = 0;
2816     my @texi_cleans;
2817     my $canonical;
2818
2819     my %texi_suffixes;
2820     foreach my $info_cursor (@texis)
2821     {
2822         my $infobase = $info_cursor;
2823         $infobase =~ s/\.(txi|texinfo|texi)$//;
2824
2825         if ($infobase eq $info_cursor)
2826         {
2827             # FIXME: report line number.
2828             &am_error ("texinfo file `$info_cursor' has unrecognized extension");
2829             next;
2830         }
2831         $texi_suffixes{$1} = 1;
2832
2833         # If 'version.texi' is referenced by input file, then include
2834         # automatic versioning capability.
2835         my ($out_file, $vtexi, @clean_files) =
2836           &scan_texinfo_file ("$relative_dir/$info_cursor")
2837             or next;
2838         push (@texi_cleans, @clean_files);
2839
2840         if ($vtexi)
2841         {
2842             &am_error ("`$vtexi', included in `$info_cursor', also included in `$versions{$vtexi}'")
2843                 if (defined $versions{$vtexi});
2844             $versions{$vtexi} = $info_cursor;
2845
2846             # We number the stamp-vti files.  This is doable since the
2847             # actual names don't matter much.  We only number starting
2848             # with the second one, so that the common case looks nice.
2849             my $vti = ($done ? $done : 'vti');
2850             ++$done;
2851
2852             &require_conf_file_with_line ('info_TEXINFOS', $FOREIGN,
2853                                           'mdate-sh');
2854
2855             my $conf_dir;
2856             if ($config_aux_dir_set_in_configure_in)
2857             {
2858                 $conf_dir = $config_aux_dir;
2859                 $conf_dir .= '/' unless $conf_dir =~ /\/$/;
2860             }
2861             else
2862             {
2863                 $conf_dir = '$(srcdir)/';
2864             }
2865             $output_rules .= &file_contents ('texi-vers',
2866                                              ('TEXI'  => $info_cursor,
2867                                               'VTI'   => $vti,
2868                                               'VTEXI' => $vtexi,
2869                                               'MDDIR' => $conf_dir));
2870         }
2871
2872         # If user specified file_TEXINFOS, then use that as explicit
2873         # dependency list.
2874         @texi_deps = ();
2875         push (@texi_deps, $info_cursor);
2876         # Prefix with $(srcdir) because some version of make won't
2877         # work if the target has it and the dependency doesn't.
2878         push (@texi_deps, '$(srcdir)/' . $vtexi) if $vtexi;
2879
2880         my $canonical = &canonicalize ($infobase);
2881         if (&variable_defined ($canonical . "_TEXINFOS"))
2882         {
2883             push (@texi_deps, '$(' . $canonical . '_TEXINFOS)');
2884             &push_dist_common ('$(' . $canonical . '_TEXINFOS)');
2885         }
2886
2887         $output_rules .= ("\n" . $out_file . ": "
2888                           . join (' ', @texi_deps)
2889                           . "\n" . $infobase . ".dvi: "
2890                           . join (' ', @texi_deps)
2891                           . "\n");
2892
2893         push (@info_deps_list, $out_file);
2894         push (@dvis_list, $infobase . '.dvi');
2895     }
2896
2897     # Handle location of texinfo.tex.
2898     my $need_texi_file = 0;
2899     my $texinfodir;
2900     if ($cygnus_mode)
2901     {
2902         $texinfodir = '$(top_srcdir)/../texinfo';
2903         &define_variable ('TEXINFO_TEX', "$texinfodir/texinfo.tex");
2904
2905     }
2906     elsif ($config_aux_dir_set_in_configure_in)
2907     {
2908         $texinfodir = $config_aux_dir;
2909         &define_variable ('TEXINFO_TEX', "$texinfodir/texinfo.tex");
2910         $need_texi_file = 2; # so that we require_conf_file later
2911     }
2912     elsif (&variable_defined ('TEXINFO_TEX'))
2913     {
2914         # The user defined TEXINFO_TEX so assume he knows what he is
2915         # doing.
2916         $texinfodir = ('$(srcdir)/'
2917                        . dirname (&variable_value ('TEXINFO_TEX')));
2918     }
2919     else
2920     {
2921         $texinfodir = '$(srcdir)';
2922         $need_texi_file = 1;
2923     }
2924
2925     foreach my $txsfx (sort keys %texi_suffixes)
2926     {
2927         $output_rules .= &file_contents ('texibuild',
2928                                          ('TEXINFODIR' => $texinfodir,
2929                                           'SUFFIX'     => $txsfx));
2930     }
2931
2932     # The return value.
2933     my $texiclean = &pretty_print_internal ("", "\t  ", @texi_cleans);
2934
2935     push (@dist_targets, 'dist-info');
2936
2937     if (! defined $options{'no-installinfo'})
2938     {
2939         # Make sure documentation is made and installed first.  Use
2940         # $(INFO_DEPS), not 'info', because otherwise recursive makes
2941         # get run twice during "make all".
2942         unshift (@all, '$(INFO_DEPS)');
2943     }
2944
2945     &define_variable ("INFO_DEPS", join (' ', @info_deps_list));
2946     &define_variable ("DVIS", join (' ', @dvis_list));
2947     # This next isn't strictly needed now -- the places that look here
2948     # could easily be changed to look in info_TEXINFOS.  But this is
2949     # probably better, in case noinst_TEXINFOS is ever supported.
2950     &define_variable ("TEXINFOS", &variable_value ('info_TEXINFOS'));
2951
2952     # Do some error checking.  Note that this file is not required
2953     # when in Cygnus mode; instead we defined TEXINFO_TEX explicitly
2954     # up above.
2955     if ($need_texi_file && ! defined $options{'no-texinfo.tex'})
2956     {
2957         if ($need_texi_file > 1)
2958         {
2959             &require_conf_file_with_line ('info_TEXINFOS', $FOREIGN,
2960                                           'texinfo.tex');
2961         }
2962         else
2963         {
2964             &require_file_with_line ('info_TEXINFOS', $FOREIGN, 'texinfo.tex');
2965         }
2966     }
2967
2968     return (1, $texiclean);
2969 }
2970
2971 # handle_texinfo ()
2972 # -----------------
2973 # Handle all Texinfo source.
2974 sub handle_texinfo
2975 {
2976     my ($do_something, $texiclean) = handle_texinfo_helper ();
2977     $output_rules .=  &file_contents ('texinfos',
2978                                       ('TEXICLEAN' => $texiclean,
2979                                        'LOCAL-TEXIS' => $do_something));
2980 }
2981
2982 # Handle any man pages.
2983 sub handle_man_pages
2984 {
2985     &am_line_error ('MANS', "`MANS' is an anachronism; use `man_MANS'")
2986         if &variable_defined ('MANS');
2987
2988     # Find all the sections in use.  We do this by first looking for
2989     # "standard" sections, and then looking for any additional
2990     # sections used in man_MANS.
2991     my (%sections, %vlist);
2992     # We handle nodist_ for uniformity.  man pages aren't distributed
2993     # by default so it isn't actually very important.
2994     foreach my $pfx ('', 'dist_', 'nodist_')
2995     {
2996         # Add more sections as needed.
2997         foreach my $section ('0'..'9', 'n', 'l')
2998         {
2999             if (&variable_defined ($pfx . 'man' . $section . '_MANS'))
3000             {
3001                 $sections{$section} = 1;
3002                 $vlist{'$(' . $pfx . 'man' . $section . '_MANS)'} = 1;
3003
3004                 &push_dist_common ('$(' . $pfx . 'man' . $section . '_MANS)')
3005                     if $pfx eq 'dist_';
3006             }
3007         }
3008
3009         if (&variable_defined ($pfx . 'man_MANS'))
3010         {
3011             $vlist{'$(' . $pfx . 'man_MANS)'} = 1;
3012             foreach (&variable_value_as_list ($pfx . 'man_MANS', 'all'))
3013             {
3014                 # A page like `foo.1c' goes into man1dir.
3015                 if (/\.([0-9a-z])([a-z]*)$/)
3016                 {
3017                     $sections{$1} = 1;
3018                 }
3019             }
3020
3021             &push_dist_common ('$(' . $pfx . 'man_MANS)')
3022                 if $pfx eq 'dist_';
3023         }
3024     }
3025
3026     return unless %sections;
3027
3028     # Now for each section, generate an install and unintall rule.
3029     # Sort sections so output is deterministic.
3030     foreach my $section (sort keys %sections)
3031     {
3032         $output_rules .= &file_contents ('mans', ('SECTION' => $section));
3033     }
3034
3035     $output_vars .= &file_contents ('mans-vars',
3036                                     ('MANS' => join (' ', sort keys %vlist)));
3037
3038     if (! defined $options{'no-installman'})
3039     {
3040         push (@all, '$(MANS)');
3041     }
3042 }
3043
3044 # Handle DATA variables.
3045 sub handle_data
3046 {
3047     &am_install_var ('-noextra', '-candist', 'data', 'DATA',
3048                      'data', 'sysconf', 'sharedstate', 'localstate',
3049                      'pkgdata', 'noinst', 'check');
3050 }
3051
3052 # Handle TAGS.
3053 sub handle_tags
3054 {
3055     my @tag_deps = ();
3056     if (&variable_defined ('SUBDIRS'))
3057     {
3058         $output_rules .= ("tags-recursive:\n"
3059                           . "\tlist=\'\$(SUBDIRS)\'; for subdir in \$\$list; do \\\n"
3060                           # Never fail here if a subdir fails; it
3061                           # isn't important.
3062                           . "\t  test \"\$\$subdir\" = . || (cd \$\$subdir"
3063                           . " && \$(MAKE) \$(AM_MAKEFLAGS) tags); \\\n"
3064                           . "\tdone\n");
3065         push (@tag_deps, 'tags-recursive');
3066         &depend ('.PHONY', 'tags-recursive');
3067     }
3068
3069     if (&saw_sources_p (1)
3070         || &variable_defined ('ETAGS_ARGS')
3071         || @tag_deps)
3072     {
3073         my $config = '';
3074         foreach my $one_hdr (@config_headers)
3075         {
3076             if ($relative_dir eq dirname ($one_hdr))
3077             {
3078                 # The config header is in this directory.  So require it.
3079                 $config .= ' ' if $config;
3080                 $config .= basename ($one_hdr);
3081             }
3082         }
3083         $output_rules .= &file_contents ('tags',
3084                                          ('CONFIG' => $config,
3085                                           'DIRS'   => join (' ', @tag_deps)));
3086         &examine_variable ('TAGS_DEPENDENCIES');
3087     }
3088     elsif (&variable_defined ('TAGS_DEPENDENCIES'))
3089     {
3090         &am_line_error ('TAGS_DEPENDENCIES',
3091                         "doesn't make sense to define `TAGS_DEPENDENCIES' without sources or `ETAGS_ARGS'");
3092     }
3093     else
3094     {
3095         # Every Makefile must define some sort of TAGS rule.
3096         # Otherwise, it would be possible for a top-level "make TAGS"
3097         # to fail because some subdirectory failed.
3098         $output_rules .= "tags: TAGS\nTAGS:\n\n";
3099     }
3100 }
3101
3102 # Handle multilib support.
3103 sub handle_multilib
3104 {
3105     return unless $seen_multilib;
3106
3107     $output_rules .= &file_contents ('multilib');
3108 }
3109
3110
3111 # $BOOLEAN
3112 # &for_dist_common ($A, $B)
3113 # -------------------------
3114 # Subroutine for &handle_dist: sort files to dist.
3115 #
3116 # We put README first because it then becomes easier to make a
3117 # Usenet-compliant shar file (in these, README must be first).
3118 #
3119 # FIXME: do more ordering of files here.
3120 sub for_dist_common
3121 {
3122     return 0
3123         if $a eq $b;
3124     return -1
3125         if $a eq 'README';
3126     return 1
3127         if $b eq 'README';
3128     return $a cmp $b;
3129 }
3130
3131
3132 # handle_dist ($MAKEFILE)
3133 # -----------------------
3134 # Handle 'dist' target.
3135 sub handle_dist
3136 {
3137     my ($makefile) = @_;
3138
3139     # `make dist' isn't used in a Cygnus-style tree.
3140     # Omit the rules so that people don't try to use them.
3141     return if $cygnus_mode;
3142
3143     # Set up maint_charset.
3144     $local_maint_charset = &variable_value ('MAINT_CHARSET')
3145         if &variable_defined ('MAINT_CHARSET');
3146     $maint_charset = $local_maint_charset
3147         if $relative_dir eq '.';
3148
3149     if (&variable_defined ('DIST_CHARSET'))
3150     {
3151         &am_line_error ('DIST_CHARSET',
3152                         "DIST_CHARSET defined but no MAINT_CHARSET defined")
3153             if ! $local_maint_charset;
3154         if ($relative_dir eq '.')
3155         {
3156             $dist_charset = &variable_value ('DIST_CHARSET')
3157         }
3158         else
3159         {
3160             &am_line_error ('DIST_CHARSET',
3161                             "DIST_CHARSET can only be defined at top level");
3162         }
3163     }
3164
3165     # Look for common files that should be included in distribution.
3166     foreach my $cfile (@common_files)
3167     {
3168         if (-f ($relative_dir . "/" . $cfile))
3169         {
3170             &push_dist_common ($cfile);
3171         }
3172     }
3173
3174     # We might copy elements from $configure_dist_common to
3175     # %dist_common if we think we need to.  If the file appears in our
3176     # directory, we would have discovered it already, so we don't
3177     # check that.  But if the file is in a subdir without a Makefile,
3178     # we want to distribute it here if we are doing `.'.  Ugly!
3179     if ($relative_dir eq '.')
3180     {
3181        foreach my $file (split (' ' , $configure_dist_common))
3182        {
3183            if (! &is_make_dir (dirname ($file)))
3184            {
3185                &push_dist_common ($file);
3186            }
3187        }
3188     }
3189
3190
3191
3192     # Files to distributed.  Don't use &variable_value_as_list
3193     # as it recursively expands `$(dist_pkgdata_DATA)' etc.
3194     check_variable_defined_unconditionally ('DIST_COMMON');
3195     my @dist_common = split (' ', variable_value ('DIST_COMMON', 'TRUE'));
3196     @dist_common = uniq (sort for_dist_common (@dist_common));
3197     pretty_print ('DIST_COMMON = ', "\t", @dist_common);
3198
3199     # Now that we've processed DIST_COMMON, disallow further attempts
3200     # to set it.
3201     $handle_dist_run = 1;
3202
3203     # Scan EXTRA_DIST to see if we need to distribute anything from a
3204     # subdir.  If so, add it to the list.  I didn't want to do this
3205     # originally, but there were so many requests that I finally
3206     # relented.
3207     if (&variable_defined ('EXTRA_DIST'))
3208     {
3209         # FIXME: This should be fixed to work with conditionals.  That
3210         # will require only making the entries in %dist_dirs under the
3211         # appropriate condition.  This is meaningful if the nature of
3212         # the distribution should depend upon the configure options
3213         # used.
3214         foreach (&variable_value_as_list ('EXTRA_DIST', ''))
3215         {
3216             next if /^\@.*\@$/;
3217             next unless s,/+[^/]+$,,;
3218             $dist_dirs{$_} = 1
3219                 unless $_ eq '.';
3220         }
3221     }
3222
3223     # We have to check DIST_COMMON for extra directories in case the
3224     # user put a source used in AC_OUTPUT into a subdir.
3225     foreach (&variable_value_as_list ('DIST_COMMON', 'all'))
3226     {
3227         next if /^\@.*\@$/;
3228         next unless s,/+[^/]+$,,;
3229         $dist_dirs{$_} = 1
3230             unless $_ eq '.';
3231     }
3232
3233     # Rule to check whether a distribution is viable.
3234     my %transform = ('DISTCHECK-HOOK' => &target_defined ('distcheck-hook'),
3235                      'GETTEXT'        => $seen_gettext);
3236
3237     # Prepend $(distdir) to each directory given.
3238     my %rewritten = map { '$(distdir)/' . "$_" => 1 } keys %dist_dirs;
3239     $transform{'DISTDIRS'} = join (' ', sort keys %rewritten);
3240
3241     # If we have SUBDIRS, create all dist subdirectories and do
3242     # recursive build.
3243     if (&variable_defined ('SUBDIRS'))
3244     {
3245         # If SUBDIRS is conditionally defined, then set DIST_SUBDIRS
3246         # to all possible directories, and use it.  If DIST_SUBDIRS is
3247         # defined, just use it.
3248         my $dist_subdir_name;
3249         # Note that we check DIST_SUBDIRS first on purpose.  At least
3250         # one project uses so many conditional subdirectories that
3251         # calling variable_conditionally_defined on SUBDIRS will cause
3252         # automake to grow to 150Mb.  Sigh.
3253         if (&variable_defined ('DIST_SUBDIRS')
3254             || variable_conditionally_defined ('SUBDIRS'))
3255         {
3256             $dist_subdir_name = 'DIST_SUBDIRS';
3257             if (! &variable_defined ('DIST_SUBDIRS'))
3258             {
3259                 &define_pretty_variable
3260                   ('DIST_SUBDIRS', '',
3261                    uniq (&variable_value_as_list ('SUBDIRS', 'all')));
3262             }
3263         }
3264         else
3265         {
3266             $dist_subdir_name = 'SUBDIRS';
3267             # We always define this because that is what `distclean'
3268             # wants.
3269             &define_pretty_variable ('DIST_SUBDIRS', '', '$(SUBDIRS)');
3270         }
3271
3272         $transform{'DIST_SUBDIR_NAME'} = $dist_subdir_name;
3273     }
3274
3275     # If the target `dist-hook' exists, make sure it is run.  This
3276     # allows users to do random weird things to the distribution
3277     # before it is packaged up.
3278     push (@dist_targets, 'dist-hook')
3279       if &target_defined ('dist-hook');
3280     $transform{'DIST-TARGETS'} = join(' ', @dist_targets);
3281
3282     # Defining $(DISTDIR).
3283     $transform{'DISTDIR'} = !&variable_defined('distdir');
3284     $transform{'TOP_DISTDIR'} = backname ($relative_dir);
3285
3286     $output_rules .= &file_contents ('distdir', %transform);
3287 }
3288
3289
3290 # Handle subdirectories.
3291 sub handle_subdirs
3292 {
3293     return
3294       unless &variable_defined ('SUBDIRS');
3295
3296     # Make sure each directory mentioned in SUBDIRS actually exists.
3297     foreach my $dir (&variable_value_as_list ('SUBDIRS', 'all'))
3298     {
3299         # Skip directories substituted by configure.
3300         next if $dir =~ /^\@.*\@$/;
3301
3302         if (! -d $am_relative_dir . '/' . $dir)
3303         {
3304             &am_line_error ('SUBDIRS',
3305                             "required directory $am_relative_dir/$dir does not exist");
3306             next;
3307         }
3308
3309         &am_line_error ('SUBDIRS', "directory should not contain `/'")
3310             if $dir =~ /\//;
3311     }
3312
3313     $output_rules .= &file_contents ('subdirs');
3314     variable_pretty_output ('RECURSIVE_TARGETS', 'TRUE');
3315 }
3316
3317
3318 # ($REGEN, @DEPENDENCIES)
3319 # &scan_aclocal_m4
3320 # ----------------
3321 # If aclocal.m4 creation is automated, return the list of its dependencies.
3322 sub scan_aclocal_m4
3323 {
3324     my $regen_aclocal = 0;
3325
3326     return (0, ())
3327       unless $relative_dir eq '.';
3328
3329     &examine_variable ('CONFIG_STATUS_DEPENDENCIES');
3330     &examine_variable ('CONFIGURE_DEPENDENCIES');
3331
3332     if (-f 'aclocal.m4')
3333     {
3334         &define_variable ("ACLOCAL_M4", '$(top_srcdir)/aclocal.m4');
3335         &push_dist_common ('aclocal.m4');
3336
3337         my $aclocal = new IO::File ("< aclocal.m4");
3338         if ($aclocal)
3339         {
3340             my $line = $aclocal->getline;
3341             $aclocal->close;
3342
3343             if ($line =~ 'generated automatically by aclocal')
3344             {
3345                 $regen_aclocal = 1;
3346             }
3347         }
3348     }
3349
3350     my @ac_deps = ();
3351
3352     if (-f 'acinclude.m4')
3353     {
3354         $regen_aclocal = 1;
3355         push @ac_deps, 'acinclude.m4';
3356     }
3357
3358     if (&variable_defined ('ACLOCAL_M4_SOURCES'))
3359     {
3360         push (@ac_deps, '$(ACLOCAL_M4_SOURCES)');
3361     }
3362     elsif (&variable_defined ('ACLOCAL_AMFLAGS'))
3363     {
3364         # Scan all -I directories for m4 files.  These are our
3365         # dependencies.
3366         my $examine_next = 0;
3367         foreach my $amdir (&variable_value_as_list ('ACLOCAL_AMFLAGS', ''))
3368         {
3369             if ($examine_next)
3370             {
3371                 $examine_next = 0;
3372                 if ($amdir !~ /^\// && -d $amdir)
3373                 {
3374                     foreach my $ac_dep (&my_glob ($amdir . '/*.m4'))
3375                     {
3376                         $ac_dep =~ s/^\.\/+//;
3377                         push (@ac_deps, $ac_dep)
3378                           unless $ac_dep eq "aclocal.m4"
3379                             || $ac_dep eq "acinclude.m4";
3380                     }
3381                 }
3382             }
3383             elsif ($amdir eq '-I')
3384             {
3385                 $examine_next = 1;
3386             }
3387         }
3388     }
3389
3390     # Note that it might be possible that aclocal.m4 doesn't exist but
3391     # should be auto-generated.  This case probably isn't very
3392     # important.
3393
3394     return ($regen_aclocal, @ac_deps);
3395 }
3396
3397 # Rewrite a list of input files into a form suitable to put on a
3398 # dependency list.  The idea is that if an input file has a directory
3399 # part the same as the current directory, then the directory part is
3400 # simply removed.  But if the directory part is different, then
3401 # $(top_srcdir) is prepended.  Among other things, this is used to
3402 # generate the dependency list for the output files generated by
3403 # AC_OUTPUT.  Consider what the dependencies should look like in this
3404 # case:
3405 #   AC_OUTPUT(src/out:src/in1:lib/in2)
3406 # The first argument, ADD_SRCDIR, is 1 if $(top_srcdir) should be added.
3407 # If 0 then files that require this addition will simply be ignored.
3408 sub rewrite_inputs_into_dependencies
3409 {
3410     my ($add_srcdir, @inputs) = @_;
3411     my @newinputs;
3412
3413     foreach my $single (@inputs)
3414     {
3415         if (dirname ($single) eq $relative_dir)
3416         {
3417             push (@newinputs, basename ($single));
3418         }
3419         elsif ($add_srcdir)
3420         {
3421             push (@newinputs, '$(top_srcdir)/' . $single);
3422         }
3423     }
3424
3425     return @newinputs;
3426 }
3427
3428 # Handle remaking and configure stuff.
3429 # We need the name of the input file, to do proper remaking rules.
3430 sub handle_configure
3431 {
3432     my ($local, $input, @secondary_inputs) = @_;
3433
3434     my $input_base = basename ($input);
3435     my $local_base = basename ($local);
3436
3437     my $amfile = $input_base . '.am';
3438     # We know we can always add '.in' because it really should be an
3439     # error if the .in was missing originally.
3440     my $infile = '$(srcdir)/' . $input_base . '.in';
3441     my $colon_infile = '';
3442     if ($local ne $input || @secondary_inputs)
3443     {
3444         $colon_infile = ':' . $input . '.in';
3445     }
3446     $colon_infile .= ':' . join (':', @secondary_inputs)
3447         if @secondary_inputs;
3448
3449     my @rewritten = &rewrite_inputs_into_dependencies (1, @secondary_inputs);
3450
3451     my ($regen_aclocal_m4, @aclocal_m4_deps) = scan_aclocal_m4 ();
3452
3453     $output_rules .=
3454       &file_contents ('configure',
3455                       ('MAKEFILE'
3456                        => $local_base,
3457                        'MAKEFILE-DEPS'
3458                        => join (' ', @rewritten),
3459                        'CONFIG-MAKEFILE'
3460                        => ((($relative_dir eq '.') ? '$@' : '$(subdir)/$@')
3461                            . $colon_infile),
3462                        'MAKEFILE-IN'
3463                        => $infile,
3464                        'MAKEFILE-IN-DEPS'
3465                        => join (' ', @include_stack),
3466                        'MAKEFILE-AM'
3467                        => $amfile,
3468                        'STRICTNESS'
3469                        => $cygnus_mode ? 'cygnus' : $strictness_name,
3470                        'USE-DEPS'
3471                        => $cmdline_use_dependencies ? '' : ' --ignore-deps',
3472                        'MAKEFILE-AM-SOURCES'
3473                        =>  "$input$colon_infile",
3474                        'REGEN-ACLOCAL-M4'
3475                        => $regen_aclocal_m4,
3476                        'ACLOCAL_M4_DEPS'
3477                        => join (' ', @aclocal_m4_deps)));
3478
3479     if ($relative_dir eq '.')
3480     {
3481         &push_dist_common ('acconfig.h')
3482             if -f 'acconfig.h';
3483     }
3484
3485     # If we have a configure header, require it.
3486     my @local_fullnames = @config_fullnames;
3487     my @local_names = @config_names;
3488     my $hdr_index = 0;
3489     my $distclean_config = '';
3490     foreach my $one_hdr (@config_headers)
3491     {
3492         my $one_fullname = shift (@local_fullnames);
3493         my $one_name = shift (@local_names);
3494         $hdr_index += 1;
3495         my $header_dir = dirname ($one_name);
3496
3497         # If the header is in the current directory we want to build
3498         # the header here.  Otherwise, if we're at the topmost
3499         # directory and the header's directory doesn't have a
3500         # Makefile, then we also want to build the header.
3501         if ($relative_dir eq $header_dir
3502             || ($relative_dir eq '.' && ! &is_make_dir ($header_dir)))
3503         {
3504             my ($cn_sans_dir, $stamp_dir);
3505             if ($relative_dir eq $header_dir)
3506             {
3507                 $cn_sans_dir = basename ($one_name);
3508                 $stamp_dir = '';
3509             }
3510             else
3511             {
3512                 $cn_sans_dir = $one_name;
3513                 if ($header_dir eq '.')
3514                 {
3515                     $stamp_dir = '';
3516                 }
3517                 else
3518                 {
3519                     $stamp_dir = $header_dir . '/';
3520                 }
3521             }
3522
3523             # Compute relative path from directory holding output
3524             # header to directory holding input header.  FIXME:
3525             # doesn't handle case where we have multiple inputs.
3526             my $ch_sans_dir;
3527             if (dirname ($one_hdr) eq $relative_dir)
3528             {
3529                 $ch_sans_dir = basename ($one_hdr);
3530             }
3531             else
3532             {
3533                 $ch_sans_dir = backname ($relative_dir) . '/' . $one_hdr;
3534             }
3535
3536             &require_file_with_conf_line ($config_header_line,
3537                                           $FOREIGN, $ch_sans_dir);
3538
3539             # Header defined and in this directory.
3540             my @files;
3541             if (-f $one_name . '.top')
3542             {
3543                 push (@files, "${cn_sans_dir}.top");
3544             }
3545             if (-f $one_name . '.bot')
3546             {
3547                 push (@files, "${cn_sans_dir}.bot");
3548             }
3549
3550             &push_dist_common (@files);
3551
3552             # For now, acconfig.h can only appear in the top srcdir.
3553             if (-f 'acconfig.h')
3554             {
3555                 push (@files, '$(top_srcdir)/acconfig.h');
3556             }
3557
3558             my $stamp_name = 'stamp-h';
3559             $stamp_name .= "${hdr_index}" if scalar (@config_headers) > 1;
3560
3561             my $out_dir = dirname ($ch_sans_dir);
3562
3563             $output_rules .=
3564               &file_contents ('remake-hdr',
3565                               ('FILES'              => join (' ', @files),
3566                                'CONFIG_HEADER'      => $cn_sans_dir,
3567                                'CONFIG_HEADER_IN'   => $ch_sans_dir,
3568                                'CONFIG_HEADER_FULL' => $one_fullname,
3569                                'STAMP'            => "$stamp_dir$stamp_name",
3570                                'SRC_STAMP'        => "$out_dir/$stamp_name"));
3571
3572             &create ("${relative_dir}/${out_dir}/${stamp_name}.in");
3573             &require_file_with_conf_line ($config_header_line, $FOREIGN,
3574                                           "${out_dir}/${stamp_name}.in");
3575
3576             $distclean_config .= ' ' if $distclean_config;
3577             $distclean_config .= $cn_sans_dir;
3578         }
3579     }
3580
3581     if ($distclean_config)
3582     {
3583         $output_rules .= &file_contents ('clean-hdr',
3584                                          ('FILES' => $distclean_config));
3585     }
3586
3587     # Set location of mkinstalldirs.
3588     &define_variable ('mkinstalldirs',
3589                       ('$(SHELL) ' . $config_aux_dir . '/mkinstalldirs'));
3590
3591     &am_line_error ('CONFIG_HEADER',
3592                     "`CONFIG_HEADER' is an anachronism; now determined from `$configure_ac'")
3593         if &variable_defined ('CONFIG_HEADER');
3594
3595     my $config_header = '';
3596     foreach my $one_name (@config_names)
3597     {
3598         # Generate CONFIG_HEADER define.
3599         my $one_hdr;
3600         if ($relative_dir eq dirname ($one_name))
3601         {
3602             $one_hdr = basename ($one_name);
3603         }
3604         else
3605         {
3606             $one_hdr = "\$(top_builddir)/${one_name}";
3607         }
3608
3609         $config_header .= ' ' if $config_header;
3610         $config_header .= $one_hdr;
3611     }
3612     if ($config_header)
3613     {
3614         &define_variable ("CONFIG_HEADER", $config_header);
3615     }
3616
3617     # Now look for other files in this directory which must be remade
3618     # by config.status, and generate rules for them.
3619     my @actual_other_files = ();
3620     foreach my $lfile (@other_input_files)
3621     {
3622         my ($file, $local);
3623         my (@inputs, @rewritten_inputs);
3624         my ($need_rewritten);
3625         if ($lfile =~ /^([^:]*):(.*)$/)
3626         {
3627             # This is the ":" syntax of AC_OUTPUT.
3628             $file = $1;
3629             $local = basename ($file);
3630             @inputs = split (':', $2);
3631             @rewritten_inputs = &rewrite_inputs_into_dependencies (1, @inputs);
3632             $need_rewritten = 1;
3633         }
3634         else
3635         {
3636             # Normal usage.
3637             $file = $lfile;
3638             $local = basename ($file);
3639             @inputs = ($file . '.in');
3640             @rewritten_inputs =
3641                 &rewrite_inputs_into_dependencies (1, @inputs);
3642             $need_rewritten = 0;
3643         }
3644
3645         # Make sure the dist directory for each input file is created.
3646         # We only have to do this at the topmost level though.  This
3647         # is a bit ugly but it easier than spreading out the logic,
3648         # especially in cases like AC_OUTPUT(foo/out:bar/in), where
3649         # there is no Makefile in bar/.
3650         if ($relative_dir eq '.')
3651         {
3652             foreach (@inputs)
3653             {
3654                 $dist_dirs{dirname ($_)} = 1;
3655             }
3656         }
3657
3658         # We skip any automake input files, as they are handled
3659         # elsewhere.  We also skip files that aren't in this
3660         # directory.  However, if the file's directory does not have a
3661         # Makefile, and we are currently doing `.', then we create a
3662         # rule to rebuild the file in the subdir.
3663         next if -f $file . '.am';
3664         my $fd = dirname ($file);
3665         if ($fd ne $relative_dir)
3666         {
3667             if ($relative_dir eq '.' && ! &is_make_dir ($fd))
3668             {
3669                 $local = $file;
3670             }
3671             else
3672             {
3673                 next;
3674             }
3675         }
3676
3677         # Some users have been tempted to put `stamp-h' in the
3678         # AC_OUTPUT line.  This won't do the right thing, so we
3679         # explicitly fail here.
3680         if ($local eq 'stamp-h')
3681         {
3682             # FIXME: allow real filename.
3683             &am_conf_error ($configure_ac, $ac_output_line,
3684                             'stamp-h should not appear in AC_OUTPUT');
3685             next;
3686         }
3687
3688         $output_rules .= ($local . ': '
3689                           . '$(top_builddir)/config.status '
3690                           . join (' ', @rewritten_inputs) . "\n"
3691                           . "\t"
3692                           . 'cd $(top_builddir) && CONFIG_FILES='
3693                           . ($relative_dir eq '.' ? '' : '$(subdir)/')
3694                           . '$@' . ($need_rewritten
3695                                     ? (':' . join (':', @inputs))
3696                                     : '')
3697                           . ' CONFIG_HEADERS= CONFIG_LINKS= $(SHELL) ./config.status'
3698                           . "\n");
3699         push (@actual_other_files, $local);
3700
3701         # Require all input files.
3702         &require_file_with_conf_line ($ac_output_line, $FOREIGN,
3703                                       &rewrite_inputs_into_dependencies (0, @inputs));
3704     }
3705
3706     # These files get removed by "make clean".
3707     &define_pretty_variable ('CONFIG_CLEAN_FILES', '', @actual_other_files);
3708 }
3709
3710 # Handle C headers.
3711 sub handle_headers
3712 {
3713     my @r = &am_install_var ('-defaultdist', 'header', 'HEADERS', 'include',
3714                              'oldinclude', 'pkginclude',
3715                              'noinst', 'check');
3716     foreach (@r)
3717     {
3718         next unless /\.(.*)$/;
3719         &saw_extension ($1);
3720     }
3721 }
3722
3723 sub handle_gettext
3724 {
3725     return if ! $seen_gettext || $relative_dir ne '.';
3726
3727     if (! &variable_defined ('SUBDIRS'))
3728     {
3729         &am_conf_error
3730             ("AM_GNU_GETTEXT used but SUBDIRS not defined");
3731         return;
3732     }
3733
3734     my @subdirs = &variable_value_as_list ('SUBDIRS', 'all');
3735     &am_line_error ('SUBDIRS',
3736                     "AM_GNU_GETTEXT used but `po' not in SUBDIRS")
3737         if ! grep ('po', @subdirs);
3738     &am_line_error ('SUBDIRS',
3739                     "AM_GNU_GETTEXT used but `intl' not in SUBDIRS")
3740         if ! grep ('intl', @subdirs);
3741
3742     &require_file_with_conf_line ($ac_gettext_line, $GNU, 'ABOUT-NLS');
3743
3744     # Ensure that each language in ALL_LINGUAS has a .po file, and
3745     # each po file is mentioned in ALL_LINGUAS.
3746     if ($seen_linguas)
3747     {
3748         my %linguas = map { $_ => 1 } split (' ', $all_linguas);
3749
3750         foreach (<po/*.po>)
3751         {
3752             s/^po\///;
3753             s/\.po$//;
3754
3755             &am_line_error ($all_linguas_line,
3756                             ("po/$_.po exists but `$_' not in `ALL_LINGUAS'"))
3757                 if ! $linguas{$_};
3758         }
3759
3760         foreach (keys %linguas)
3761         {
3762             &am_line_error ($all_linguas_line,
3763                             "$_ in `ALL_LINGUAS' but po/$_.po does not exist")
3764                 if ! -f "po/$_.po";
3765         }
3766     }
3767     else
3768     {
3769         &am_error ("AM_GNU_GETTEXT in `$configure_ac' but `ALL_LINGUAS' not defined");
3770     }
3771 }
3772
3773 # Handle footer elements.
3774 sub handle_footer
3775 {
3776     # NOTE don't use define_pretty_variable here, because
3777     # $contents{...} is already defined.
3778     $output_vars .= 'SOURCES = ' . variable_value ('SOURCES') . "\n\n"
3779       if variable_value ('SOURCES');
3780
3781
3782     &am_line_error ('.SUFFIXES',
3783                     "use variable `SUFFIXES', not target `.SUFFIXES'")
3784       if target_defined ('.SUFFIXES');
3785
3786     # Note: AIX 4.1 /bin/make will fail if any suffix rule appears
3787     # before .SUFFIXES.  So we make sure that .SUFFIXES appears before
3788     # anything else, by sticking it right after the default: target.
3789     $output_header .= ".SUFFIXES:\n";
3790     if (@suffixes || &variable_defined ('SUFFIXES'))
3791     {
3792         # Make sure suffixes has unique elements.  Sort them to ensure
3793         # the output remains consistent.  However, $(SUFFIXES) is
3794         # always at the start of the list, unsorted.  This is done
3795         # because make will choose rules depending on the ordering of
3796         # suffixes, and this lets the user have some control.  Push
3797         # actual suffixes, and not $(SUFFIXES).  Some versions of make
3798         # do not like variable substitutions on the .SUFFIXES line.
3799         my @user_suffixes = (&variable_defined ('SUFFIXES')
3800                              ? &variable_value_as_list ('SUFFIXES', '')
3801                              : ());
3802
3803         my %suffixes = map { $_ => 1 } @suffixes;
3804         delete @suffixes{@user_suffixes};
3805
3806         $output_header .= (".SUFFIXES: "
3807                            . join (' ', @user_suffixes, sort keys %suffixes)
3808                            . "\n");
3809     }
3810
3811     $output_trailer .= file_contents ('footer');
3812 }
3813
3814 # Deal with installdirs target.
3815 sub handle_installdirs ()
3816 {
3817     $output_rules .=
3818       &file_contents ('install',
3819                       ('_am_installdirs'
3820                        => variable_value ('_am_installdirs') || ''));
3821 }
3822
3823
3824 # Deal with all and all-am.
3825 sub handle_all ($)
3826 {
3827     my ($makefile) = @_;
3828
3829     # Output `all-am'.
3830
3831     # Put this at the beginning for the sake of non-GNU makes.  This
3832     # is still wrong if these makes can run parallel jobs.  But it is
3833     # right enough.
3834     unshift (@all, basename ($makefile));
3835
3836     foreach my $one_name (@config_names)
3837     {
3838         push (@all, basename ($one_name))
3839             if dirname ($one_name) eq $relative_dir;
3840     }
3841
3842     # Install `all' hooks.
3843     if (&target_defined ("all-local"))
3844     {
3845       push (@all, "all-local");
3846       &depend ('.PHONY', "all-local");
3847     }
3848
3849     &pretty_print_rule ("all-am:", "\t\t", @all);
3850     &depend ('.PHONY', 'all-am', 'all');
3851
3852
3853     # Output `all'.
3854
3855     my @local_headers = ();
3856     push @local_headers, '$(BUILT_SOURCES)'
3857       if &variable_defined ('BUILT_SOURCES');
3858     foreach my $one_name (@config_names)
3859       {
3860         push @local_headers, basename ($one_name)
3861           if dirname ($one_name) eq $relative_dir;
3862       }
3863
3864     if (@local_headers)
3865       {
3866         # We need to make sure config.h is built before we recurse.
3867         # We also want to make sure that built sources are built
3868         # before any ordinary `all' targets are run.  We can't do this
3869         # by changing the order of dependencies to the "all" because
3870         # that breaks when using parallel makes.  Instead we handle
3871         # things explicitly.
3872         $output_all .= ("all: " . join (' ', @local_headers)
3873                         . "\n\t"
3874                         . '$(MAKE) $(AM_MAKEFLAGS) '
3875                         . (&variable_defined ('SUBDIRS')
3876                            ? 'all-recursive' : 'all-am')
3877                         . "\n\n");
3878       }
3879     else
3880       {
3881         $output_all .= "all: " . (&variable_defined ('SUBDIRS')
3882                                   ? 'all-recursive' : 'all-am') . "\n\n";
3883       }
3884 }
3885
3886
3887 # Handle check merge target specially.
3888 sub do_check_merge_target
3889 {
3890     if (&target_defined ('check-local'))
3891     {
3892         # User defined local form of target.  So include it.
3893         push (@check_tests, 'check-local');
3894         &depend ('.PHONY', 'check-local');
3895     }
3896
3897     # In --cygnus mode, check doesn't depend on all.
3898     if ($cygnus_mode)
3899     {
3900         # Just run the local check rules.
3901         &pretty_print_rule ('check-am:', "\t\t", @check);
3902     }
3903     else
3904     {
3905         # The check target must depend on the local equivalent of
3906         # `all', to ensure all the primary targets are built.  Then it
3907         # must build the local check rules.
3908         $output_rules .= "check-am: all-am\n";
3909         &pretty_print_rule ("\t\$(MAKE) \$(AM_MAKEFLAGS)", "\t  ",
3910                             @check)
3911             if @check;
3912     }
3913     &pretty_print_rule ("\t\$(MAKE) \$(AM_MAKEFLAGS)", "\t  ",
3914                         @check_tests)
3915         if @check_tests;
3916
3917     &depend ('.PHONY', 'check', 'check-am');
3918     $output_rules .= ("check: "
3919                       . (&variable_defined ('SUBDIRS')
3920                          ? 'check-recursive' : 'check-am')
3921                       . "\n");
3922 }
3923
3924 # Handle all 'clean' targets.
3925 sub handle_clean
3926 {
3927     my %transform;
3928
3929     # Don't include `MAINTAINER'; it is handled specially below.
3930     foreach my $name ('MOSTLY', '', 'DIST')
3931     {
3932       $transform{"${name}CLEAN"} = &variable_defined ("${name}CLEANFILES");
3933     }
3934
3935     # Built sources are automatically removed by maintainer-clean.
3936     push (@maintainer_clean_files, '$(BUILT_SOURCES)')
3937         if &variable_defined ('BUILT_SOURCES');
3938     push (@maintainer_clean_files, '$(MAINTAINERCLEANFILES)')
3939         if &variable_defined ('MAINTAINERCLEANFILES');
3940
3941     $output_rules .= &file_contents ('clean',
3942                                      (%transform,
3943                                       'MCFILES'
3944                                       # Join with no space to avoid
3945                                       # spurious `test -z' success at
3946                                       # runtime.
3947                                       => join ('', @maintainer_clean_files),
3948                                       'MFILES'
3949                                       # A space is required in the join here.
3950                                       => join (' ', @maintainer_clean_files)));
3951 }
3952
3953
3954 # &depend ($CATEGORY, @DEPENDENDEES)
3955 # ----------------------------------
3956 # The target $CATEGORY depends on @DEPENDENDEES.
3957 sub depend
3958 {
3959     my ($category, @dependendees) = @_;
3960     {
3961       push (@{$dependencies{$category}}, @dependendees);
3962     }
3963 }
3964
3965
3966 # &target_cmp ($A, $B)
3967 # --------------------
3968 # Subroutine for &handle_factored_dependencies to let `.PHONY' be last.
3969 sub target_cmp
3970 {
3971     return 0
3972         if $a eq $b;
3973     return -1
3974         if $b eq '.PHONY';
3975     return 1
3976         if $a eq '.PHONY';
3977     return $a cmp $b;
3978 }
3979
3980
3981 # &handle_factored_dependencies ()
3982 # --------------------------------
3983 # Handle everything related to gathered targets.
3984 sub handle_factored_dependencies
3985 {
3986     # Reject bad hooks.
3987     foreach my $utarg ('uninstall-data-local', 'uninstall-data-hook',
3988                        'uninstall-exec-local', 'uninstall-exec-hook')
3989     {
3990         if (&target_defined ($utarg))
3991         {
3992             my $x = $utarg;
3993             $x =~ s/(data|exec)-//;
3994             &am_line_error ($utarg, "use `$x', not `$utarg'");
3995         }
3996     }
3997
3998     if (&target_defined ('install-local'))
3999     {
4000         &am_line_error ('install-local',
4001                         "use `install-data-local' or `install-exec-local', "
4002                         . "not `install-local'");
4003     }
4004
4005     if (!defined $options{'no-installinfo'}
4006         && &target_defined ('install-info-local'))
4007     {
4008         &am_line_error ('install-info-local',
4009                         "`install-info-local' target defined but "
4010                         . "`no-installinfo' option not in use");
4011     }
4012
4013     # Install the -local hooks.
4014     foreach (keys %dependencies)
4015     {
4016       # Hooks are installed on the -am targets.
4017       s/-am$// or next;
4018       if (&target_defined ("$_-local"))
4019         {
4020           depend ("$_-am", "$_-local");
4021           &depend ('.PHONY', "$_-local");
4022         }
4023     }
4024
4025     # Install the -hook hooks.
4026     # FIXME: Why not be as liberal as we are with -local hooks?
4027     foreach ('install-exec', 'install-data')
4028     {
4029       if (&target_defined ("$_-hook"))
4030         {
4031           $actions{"$_-am"} .=
4032             ("\t\@\$(NORMAL_INSTALL)\n"
4033              . "\t" . '$(MAKE) $(AM_MAKEFLAGS) ' . "$_-hook\n");
4034         }
4035     }
4036
4037     # All the required targets are phony.
4038     depend ('.PHONY', keys %required_targets);
4039
4040     # Actually output gathered targets.
4041     foreach (sort target_cmp keys %dependencies)
4042     {
4043         # If there is nothing about this guy, skip it.
4044         next
4045           unless (@{$dependencies{$_}}
4046                   || $actions{$_}
4047                   || $required_targets{$_});
4048         &pretty_print_rule ("$_:", "\t",
4049                             uniq (sort @{$dependencies{$_}}));
4050         $output_rules .= $actions{$_}
4051           if defined $actions{$_};
4052         $output_rules .= "\n";
4053     }
4054 }
4055
4056
4057 # &handle_tests_dejagnu ()
4058 # ------------------------
4059 sub handle_tests_dejagnu
4060 {
4061     push (@check_tests, 'check-DEJAGNU');
4062
4063     # Only create site.exp rule if user hasn't already written one.
4064     $output_rules .=
4065       file_contents ('dejagnu', ('SITE-EXP' => ! target_defined ('site.exp')));
4066 }
4067
4068
4069 # Handle TESTS variable and other checks.
4070 sub handle_tests
4071 {
4072     if (defined $options{'dejagnu'})
4073     {
4074         &handle_tests_dejagnu;
4075     }
4076     else
4077     {
4078         foreach my $c ('DEJATOOL', 'RUNTEST', 'RUNTESTFLAGS')
4079         {
4080             &am_line_error ($c,
4081                             "`$c' defined but `dejagnu' not in `AUTOMAKE_OPTIONS'")
4082               if &variable_defined ($c);
4083         }
4084     }
4085
4086     if (&variable_defined ('TESTS'))
4087     {
4088         push (@check_tests, 'check-TESTS');
4089         $output_rules .= &file_contents ('check');
4090     }
4091 }
4092
4093 # Handle Emacs Lisp.
4094 sub handle_emacs_lisp
4095 {
4096     my @elfiles = &am_install_var ('-candist', 'lisp', 'LISP',
4097                                    'lisp', 'noinst');
4098
4099     return if ! @elfiles;
4100
4101     # Generate .elc files.
4102     my @elcfiles = map { $_ . 'c' } @elfiles;
4103     &define_pretty_variable ('ELCFILES', '', @elcfiles);
4104
4105     push (@all, '$(ELCFILES)');
4106
4107     &am_error ("`lisp_LISP' defined but `AM_PATH_LISPDIR' not in `$configure_ac'")
4108       if ! $seen_lispdir && &variable_defined ('lisp_LISP');
4109
4110     &require_file_with_conf_line ('AM_PATH_LISPDIR', $FOREIGN, 'elisp-comp');
4111 }
4112
4113 # Handle Python
4114 sub handle_python
4115 {
4116     my @pyfiles = &am_install_var ('-defaultdist', 'python', 'PYTHON',
4117                                    'python', 'noinst');
4118     return if ! @pyfiles;
4119
4120     # Found some python.
4121     &am_error ("`python_PYTHON' defined but `AM_PATH_PYTHON' not in `$configure_ac'")
4122         if ! $seen_pythondir && &variable_defined ('python_PYTHON');
4123
4124     &require_file_with_conf_line ('AM_PATH_PYTHON', $FOREIGN, 'py-compile');
4125     &define_variable ('py_compile', $config_aux_dir . '/py-compile');
4126 }
4127
4128 # Handle Java.
4129 sub handle_java
4130 {
4131     my @sourcelist = &am_install_var ('-candist',
4132                                       'java', 'JAVA',
4133                                       'java', 'noinst', 'check');
4134     return if ! @sourcelist;
4135
4136     my %valid = &am_primary_prefixes ('JAVA', 1,
4137                                       'java', 'noinst', 'check');
4138
4139     my $dir;
4140     foreach my $curs (keys %valid)
4141     {
4142         if (! &variable_defined ($curs . '_JAVA') || $curs eq 'EXTRA')
4143         {
4144             next;
4145         }
4146
4147         if (defined $dir)
4148         {
4149             &am_line_error ($curs . '_JAVA',
4150                             "multiple _JAVA primaries in use");
4151         }
4152         $dir = $curs;
4153     }
4154
4155     push (@all, 'class' . $dir . '.stamp');
4156 }
4157
4158
4159 # Handle some of the minor options.
4160 sub handle_minor_options
4161 {
4162     if (defined $options{'readme-alpha'})
4163     {
4164         if ($relative_dir eq '.')
4165         {
4166             if ($package_version !~ /^$GNITS_VERSION_PATTERN$/)
4167             {
4168                 # FIXME: allow real filename.
4169                 &am_conf_line_error ($configure_ac,
4170                                      $package_version_line,
4171                                      "version `$package_version' doesn't follow Gnits standards");
4172             }
4173             elsif (defined $1 && -f 'README-alpha')
4174             {
4175                 # This means we have an alpha release.  See
4176                 # GNITS_VERSION_PATTERN for details.
4177                 &require_file ($FOREIGN, 'README-alpha');
4178             }
4179         }
4180     }
4181 }
4182
4183 ################################################################
4184
4185 my %make_list;
4186 my @make_input_list;
4187 # &scan_autoconf_config_files ($CONFIG-FILES)
4188 # -------------------------------------------
4189 # Study $CONFIG-FILES which is the first argument to AC_CONFIG_FILES
4190 # (or AC_OUTPUT).
4191 sub scan_autoconf_config_files
4192 {
4193     my ($config_files) = @_;
4194     # Look at potential Makefile.am's.
4195     foreach (split ' ', $config_files)
4196     {
4197         # Must skip empty string for Perl 4.
4198         next if $_ eq "\\" || $_ eq '';
4199
4200         # Handle $local:$input syntax.  Note that we ignore
4201         # every input file past the first, though we keep
4202         # those around for later.
4203         my ($local, $input, @rest) = split (/:/);
4204         if (! $input)
4205         {
4206             $input = $local;
4207         }
4208         else
4209         {
4210             # FIXME: should be error if .in is missing.
4211             $input =~ s/\.in$//;
4212         }
4213
4214         if (-f $input . '.am')
4215         {
4216             # We have a file that automake should generate.
4217             push (@make_input_list, $input);
4218             $make_list{$input} = join (':', ($local, @rest));
4219         }
4220         else
4221         {
4222             # We have a file that automake should cause to be
4223             # rebuilt, but shouldn't generate itself.
4224             push (@other_input_files, $_);
4225         }
4226     }
4227 }
4228
4229
4230 # &scan_autoconf_traces ($FILENAME)
4231 # ---------------------------------
4232 # FIXME: For the time being, we don't care about the FILENAME.
4233 sub scan_autoconf_traces
4234 {
4235     my ($filename) = @_;
4236
4237     my $traces = "$ENV{amtraces} ";
4238
4239     $traces .= ' -t AC_CONFIG_FILES';
4240     $traces .= ' -t AC_LIBSOURCE';
4241     $traces .= ' -t AC_SUBST';
4242
4243     my $tracefh = new IO::File ("$traces |");
4244     if (! $tracefh)
4245     {
4246         die "$me: couldn't open `$traces': $!\n";
4247     }
4248     print "$me: reading $traces\n" if $verbose;
4249
4250     while ($_ = $tracefh->getline)
4251     {
4252         chomp;
4253         my ($file, $line, $macro, @args) = split /:/;
4254         my $here = "$file:$line";
4255
4256         # Alphabetical ordering please.
4257         if ($macro eq 'AC_CONFIG_FILES')
4258         {
4259             # Look at potential Makefile.am's.
4260             &scan_autoconf_config_files ($args[0]);
4261         }
4262         elsif ($macro eq 'AC_LIBSOURCE')
4263         {
4264             my $source = "$args[0].c";
4265             # We should actually also `close' the sources: getopt.c
4266             # wants getopt.h etc.  But actually it should be done in the
4267             # macro itself, i.e., we have to first fix Autoconf to extend
4268             # _AC_LIBOBJ_DECL and use it the in various macros.
4269             if (!defined $libsources{$source})
4270                 {
4271                     print STDERR "traces: discovered $source\n";
4272                     $libsources{$source} = $here;
4273                 }
4274         }
4275         elsif ($macro eq 'AC_SUBST')
4276         {
4277             if (!defined $configure_vars{$args[0]})
4278                 {
4279                     print STDERR "traces: discovered AC_SUBST($args[0])\n";
4280                     $configure_vars{$args[0]} = $here;
4281                 }
4282         }
4283     }
4284
4285     $tracefh->close
4286         || die "$me: close: $traces: $!\n";
4287 }
4288
4289
4290 # &scan_one_autoconf_file ($FILENAME)
4291 # -----------------------------------
4292 # Scan one file for interesting things.  Subroutine of
4293 # &scan_autoconf_files.
4294 sub scan_one_autoconf_file
4295 {
4296     my ($filename) = @_;
4297
4298     my $configfh = new IO::File ("< $filename");
4299     if (! $configfh)
4300     {
4301         die "$me: couldn't open `$filename': $!\n";
4302     }
4303     print "$me: reading $filename\n" if $verbose;
4304
4305     my ($in_ac_output, $in_ac_replace) = (0, 0);
4306     while ($_ = $configfh->getline)
4307     {
4308         # Remove comments from current line.
4309         s/\bdnl\b.*$//;
4310         s/\#.*$//;
4311
4312         # Skip macro definitions.  Otherwise we might be confused into
4313         # thinking that a macro that was only defined was actually
4314         # used.
4315         next if /AC_DEFUN/;
4316
4317         # Follow includes.  This is a weirdness commonly in use at
4318         # Cygnus and hopefully nowhere else.
4319         if (/sinclude\((.*)\)/ && -f $1)
4320         {
4321             # $_ being local, if we don't preserve it, when coming
4322             # back we will have $_ undefined, which is bad for the
4323             # the rest of this routine.
4324             my $underscore = $_;
4325             &scan_one_autoconf_file ($1);
4326             $_ = $underscore;
4327         }
4328
4329         # Populate libobjs array.
4330         if (/AC_FUNC_ALLOCA/)
4331         {
4332             $libsources{'alloca.c'} = 1;
4333         }
4334         elsif (/AC_FUNC_GETLOADAVG/)
4335         {
4336             $libsources{'getloadavg.c'} = 1;
4337         }
4338         elsif (/AC_FUNC_MEMCMP/)
4339         {
4340             $libsources{'memcmp.c'} = 1;
4341         }
4342         elsif (/AC_STRUCT_ST_BLOCKS/)
4343         {
4344             $libsources{'fileblocks.c'} = 1;
4345         }
4346         elsif (/A[CM]_REPLACE_GNU_GETOPT/)
4347         {
4348             $libsources{'getopt.c'} = 1;
4349             $libsources{'getopt1.c'} = 1;
4350         }
4351         elsif (/AM_FUNC_STRTOD/)
4352         {
4353             $libsources{'strtod.c'} = 1;
4354         }
4355         elsif (/AM_WITH_REGEX/)
4356         {
4357             $libsources{'rx.c'} = 1;
4358             $libsources{'rx.h'} = 1;
4359             $libsources{'regex.c'} = 1;
4360             $libsources{'regex.h'} = 1;
4361         }
4362         elsif (/AC_FUNC_MKTIME/)
4363         {
4364             $libsources{'mktime.c'} = 1;
4365         }
4366         elsif (/AM_FUNC_ERROR_AT_LINE/)
4367         {
4368             $libsources{'error.c'} = 1;
4369             $libsources{'error.h'} = 1;
4370         }
4371         elsif (/AM_FUNC_OBSTACK/)
4372         {
4373             $libsources{'obstack.c'} = 1;
4374             $libsources{'obstack.h'} = 1;
4375         }
4376         elsif (/LIBOBJS="(.*)\s+\$LIBOBJS"/
4377                || /LIBOBJS="\$LIBOBJS\s+(.*)"/)
4378         {
4379             foreach my $libobj_iter (split (' ', $1))
4380             {
4381                 if ($libobj_iter =~ /^(.*)\.o(bj)?$/
4382                     || $libobj_iter =~ /^(.*)\.\$ac_objext$/
4383                     || $libobj_iter =~ /^(.*)\.\$\{ac_objext\}$/)
4384                 {
4385                     $libsources{$1 . '.c'} = 1;
4386                 }
4387             }
4388         }
4389         elsif (/AC_LIBOBJ\(([^)]+)\)/)
4390         {
4391             $libsources{"$1.c"} = 1;
4392         }
4393
4394         if (! $in_ac_replace && s/AC_REPLACE_FUNCS\s*\(\[?//)
4395         {
4396             $in_ac_replace = 1;
4397         }
4398         if ($in_ac_replace)
4399         {
4400             $in_ac_replace = 0 if s/[\]\)].*$//;
4401             # Remove trailing backslash.
4402             s/\\$//;
4403             foreach (split)
4404             {
4405                 # Need to skip empty elements for Perl 4.
4406                 next if $_ eq '';
4407                 $libsources{$_ . '.c'} = 1;
4408             }
4409         }
4410
4411         if (/$obsolete_rx/o)
4412         {
4413             my $hint = '';
4414             if ($obsolete_macros{$1} ne '')
4415             {
4416                 $hint = '; ' . $obsolete_macros{$1};
4417             }
4418             &am_conf_line_error ($filename, $., "`$1' is obsolete$hint");
4419         }
4420
4421         # Process the AC_OUTPUT and AC_CONFIG_FILES macros.
4422         if (! $in_ac_output && s/AC_(OUTPUT|CONFIG_FILES)\s*\(\[?//)
4423         {
4424             $in_ac_output = 1;
4425             $ac_output_line = $.;
4426         }
4427         if ($in_ac_output)
4428         {
4429             my $closing = 0;
4430             if (s/[\]\),].*$//)
4431             {
4432                 $in_ac_output = 0;
4433                 $closing = 1;
4434             }
4435
4436             # Look at potential Makefile.am's.
4437             &scan_autoconf_config_files ($_);
4438
4439             if ($closing && @make_input_list == 0 && @other_input_files == 0)
4440             {
4441                 &am_conf_line_error ($filename, $ac_output_line,
4442                                      "No files mentioned in `AC_OUTPUT'");
4443                 exit 1;
4444             }
4445         }
4446
4447         if (/$AC_CONFIG_AUX_DIR_PATTERN/o)
4448         {
4449             @config_aux_path = &unquote_m4_arg ($1);
4450             $config_aux_dir_set_in_configure_in = 1;
4451         }
4452
4453         # Check for ansi2knr.
4454         $am_c_prototypes = 1 if /AM_C_PROTOTYPES/;
4455
4456         # Check for exe extension stuff.
4457         if (/AC_EXEEXT/)
4458         {
4459             $seen_exeext = 1;
4460             $configure_vars{'EXEEXT'} = $filename . ':' . $.;
4461         }
4462
4463         if (/AC_OBJEXT/)
4464         {
4465             $seen_objext = 1;
4466             $configure_vars{'OBJEXT'} = $filename . ':' . $.;
4467         }
4468
4469         # Check for `-c -o' code.
4470         $seen_cc_c_o = 1 if /AM_PROG_CC_C_O/;
4471
4472         # Check for NLS support.
4473         if (/AM_GNU_GETTEXT/)
4474         {
4475             $seen_gettext = 1;
4476             $ac_gettext_line = $.;
4477         }
4478
4479         # Look for ALL_LINGUAS.
4480         if (/ALL_LINGUAS="(.*)"$/ || /ALL_LINGUAS=(.*)$/)
4481         {
4482             $seen_linguas = 1;
4483             $all_linguas = $1;
4484             $all_linguas_line = $.;
4485         }
4486
4487         # Handle configuration headers.  A config header of `[$1]'
4488         # means we are actually scanning AM_CONFIG_HEADER from
4489         # aclocal.m4.
4490         if (/A([CM])_CONFIG_HEADERS?\s*\((.*)\)/
4491             && $2 ne '[$1]')
4492         {
4493             &am_conf_line_error
4494                 ($filename, $., "`automake requires `AM_CONFIG_HEADER', not `AC_CONFIG_HEADER'")
4495                     if $1 eq 'C';
4496
4497             $config_header_line = $.;
4498             foreach my $one_hdr (split (' ', &unquote_m4_arg ($2)))
4499             {
4500                 push (@config_fullnames, $one_hdr);
4501                 if ($one_hdr =~ /^([^:]+):(.+)$/)
4502                 {
4503                     push (@config_names, $1);
4504                     push (@config_headers, $2);
4505                 }
4506                 else
4507                 {
4508                     push (@config_names, $one_hdr);
4509                     push (@config_headers, $one_hdr . '.in');
4510                 }
4511             }
4512         }
4513
4514         # Handle AC_CANONICAL_*.  Always allow upgrading to
4515         # AC_CANONICAL_SYSTEM, but never downgrading.
4516         $seen_canonical = $AC_CANONICAL_HOST
4517             if ! $seen_canonical
4518                 && (/AC_CANONICAL_HOST/ || /AC_CHECK_TOOL/);
4519         $seen_canonical = $AC_CANONICAL_SYSTEM if /AC_CANONICAL_SYSTEM/;
4520
4521         # If using X, include some extra variable definitions.  NOTE
4522         # we don't want to force these into CFLAGS or anything,
4523         # because not all programs will necessarily use X.
4524         if (/AC_PATH_XTRA/)
4525           {
4526             foreach my $var ('X_CFLAGS', 'X_LIBS', 'X_EXTRA_LIBS',
4527                              'X_PRE_LIBS')
4528               {
4529                 $configure_vars{$var} = $filename . ':' . $.
4530               }
4531           }
4532
4533         # This macro handles several different things.
4534         if (/$AM_INIT_AUTOMAKE_PATTERN/o)
4535         {
4536             ($package_version = $1) =~ s/$AM_PACKAGE_VERSION_PATTERN/$1/o;
4537             $package_version_line = $.;
4538             $seen_init_automake = 1;
4539         }
4540
4541         if (/AM_PROG_LEX/)
4542         {
4543             $configure_vars{'LEX'} = $filename . ':' . $.;
4544             $seen_decl_yytext = 1;
4545         }
4546         if (/AC_DECL_YYTEXT/ && $filename =~ /configure\.(ac|in)$/)
4547         {
4548             &am_conf_line_warning ($filename, $., "`AC_DECL_YYTEXT' is covered by `AM_PROG_LEX'");
4549         }
4550         if (/AC_PROG_LEX/ && $filename =~ /configure\.(ac|in)$/)
4551         {
4552             &am_conf_line_warning ($filename, $., "automake requires `AM_PROG_LEX', not `AC_PROG_LEX'");
4553         }
4554
4555         if (/AC_PROG_(F77|YACC|RANLIB|CC|CXXCPP|CXX|LEX|AWK|CPP|LN_S)/)
4556         {
4557             $configure_vars{$1} = $filename . ':' . $.;
4558         }
4559         if (/$AC_CHECK_PATTERN/o)
4560         {
4561             $configure_vars{$3} = $filename . ':' . $.;
4562         }
4563         if (/$AM_MISSING_PATTERN/o
4564             && $1 ne 'ACLOCAL'
4565             && $1 ne 'AUTOCONF'
4566             && $1 ne 'AUTOMAKE'
4567             && $1 ne 'AUTOHEADER'
4568             # AM_INIT_AUTOMAKE is AM_MISSING_PROG'ing MAKEINFO.  But
4569             # we handle it elsewhere.
4570             && $1 ne 'MAKEINFO')
4571         {
4572             $configure_vars{$1} = $filename . ':' . $.;
4573         }
4574
4575         # Explicitly avoid ANSI2KNR -- we AC_SUBST that in protos.m4,
4576         # but later define it elsewhere.  This is pretty hacky.  We
4577         # also explicitly avoid INSTALL_SCRIPT and some other
4578         # variables because they are defined in header-vars.am.
4579         # FIXME.
4580         if (/$AC_SUBST_PATTERN/o
4581             && $1 ne 'ANSI2KNR'
4582             && $1 ne 'INSTALL_SCRIPT'
4583             && $1 ne 'INSTALL_DATA')
4584         {
4585             $configure_vars{$1} = $filename . ':' . $.;
4586         }
4587
4588         $seen_decl_yytext = 1 if /AC_DECL_YYTEXT/;
4589         if (/AM_MAINTAINER_MODE/)
4590         {
4591             $seen_maint_mode = 1;
4592             $configure_cond{'MAINTAINER_MODE'} = 1;
4593         }
4594
4595         $seen_lispdir = 1 if /AM_PATH_LISPDIR/;
4596
4597         if (/AM_PATH_PYTHON/)
4598           {
4599             $seen_pythondir = 1;
4600             $configure_vars{'pythondir'} = $filename . ':' . $.;
4601             $configure_vars{'PYTHON'} = $filename . ':' . $.;
4602           }
4603
4604         if (/A(C|M)_PROG_LIBTOOL/)
4605         {
4606             # We're not ready for this yet.  People still use a
4607             # libtool with no AC_PROG_LIBTOOL.  Once that is the
4608             # dominant version we can reenable this code -- but next
4609             # time by mentioning the macro in %obsolete_macros, both
4610             # here and in aclocal.in.
4611
4612             # if (/AM_PROG_LIBTOOL/)
4613             # {
4614             #   &am_conf_line_warning ($filename, $., "`AM_PROG_LIBTOOL' is obsolete, use `AC_PROG_LIBTOOL' instead");
4615             # }
4616             $seen_libtool = 1;
4617             $libtool_line = $.;
4618             $configure_vars{'LIBTOOL'} = $filename . ':' . $.;
4619             $configure_vars{'RANLIB'} = $filename . ':' . $.;
4620             $configure_vars{'CC'} = $filename . ':' . $.;
4621             # AC_PROG_LIBTOOL runs AC_CANONICAL_HOST.  Make sure we
4622             # never downgrade (if we've seen AC_CANONICAL_SYSTEM).
4623             $seen_canonical = $AC_CANONICAL_HOST if ! $seen_canonical;
4624         }
4625
4626         $seen_multilib = 1 if (/AM_ENABLE_MULTILIB/);
4627
4628         if (/$AM_CONDITIONAL_PATTERN/o)
4629         {
4630             $configure_cond{$1} = 1;
4631         }
4632
4633         # Check for Fortran 77 intrinsic and run-time libraries.
4634         if (/AC_F77_LIBRARY_LDFLAGS/)
4635         {
4636             $configure_vars{'FLIBS'} = $filename . ':' . $.;
4637         }
4638     }
4639
4640     $configfh->close;
4641 }
4642
4643
4644 # &scan_autoconf_files ()
4645 # -----------------------
4646 # Check whether we use `configure.ac' or `configure.in'.
4647 # Scan it (and possibly `aclocal.m4') for interesting things.
4648 # We must scan aclocal.m4 because there might be AC_SUBSTs and such there.
4649 sub scan_autoconf_files
4650 {
4651     # Reinitialize libsources here.  This isn't really necessary,
4652     # since we currently assume there is only one configure.ac.  But
4653     # that won't always be the case.
4654     %libsources = ();
4655
4656     warn "$me: both `configure.ac' and `configure.in' present:"
4657          . " ignoring `configure.in'\n"
4658         if -f 'configure.ac' && -f 'configure.in';
4659     $configure_ac = 'configure.in'
4660         if -f 'configure.in';
4661     $configure_ac = 'configure.ac'
4662         if -f 'configure.ac';
4663     die "$me: `configure.ac' or `configure.in' is required\n"
4664         if !$configure_ac;
4665
4666     &scan_one_autoconf_file ($configure_ac);
4667     &scan_one_autoconf_file ('aclocal.m4')
4668         if -f 'aclocal.m4';
4669
4670     if (defined $ENV{'amtraces'})
4671     {
4672         warn '$me: Autoconf traces is an experimental feature';
4673         warn '$me: use at your own risks';
4674
4675         &scan_autoconf_traces ($configure_ac);
4676     }
4677
4678     # Set input and output files if not specified by user.
4679     if (! @input_files)
4680     {
4681         @input_files = @make_input_list;
4682         %output_files = %make_list;
4683     }
4684
4685     @configure_input_files = @make_input_list;
4686
4687     &am_conf_error ("`AM_INIT_AUTOMAKE' must be used")
4688         if ! $seen_init_automake;
4689
4690     # Look for some files we need.  Always check for these.  This
4691     # check must be done for every run, even those where we are only
4692     # looking at a subdir Makefile.  We must set relative_dir so that
4693     # the file-finding machinery works.
4694     # FIXME: Is this broken because it needs dynamic scopes.
4695     # My tests seems to show it's not the case.
4696     $relative_dir = '.';
4697     &require_config_file ($FOREIGN, 'install-sh', 'mkinstalldirs', 'missing');
4698     &am_error ("`install.sh' is an anachronism; use `install-sh' instead")
4699         if -f $config_aux_path[0] . '/install.sh';
4700
4701     &require_config_file ($FOREIGN, 'py-compile')
4702         if $seen_pythondir;
4703
4704     # Preserve dist_common for later.
4705     $configure_dist_common = variable_value ('DIST_COMMON', 'TRUE') || '';
4706 }
4707
4708 ################################################################
4709
4710 # Set up for Cygnus mode.
4711 sub check_cygnus
4712 {
4713     return unless $cygnus_mode;
4714
4715     &set_strictness ('foreign');
4716     $options{'no-installinfo'} = 1;
4717     $options{'no-dependencies'} = 1;
4718     $use_dependencies = 0;
4719
4720     if (! $seen_maint_mode)
4721     {
4722         &am_conf_error ("`AM_MAINTAINER_MODE' required when --cygnus specified");
4723     }
4724 }
4725
4726 # Do any extra checking for GNU standards.
4727 sub check_gnu_standards
4728 {
4729     if ($relative_dir eq '.')
4730     {
4731         # In top level (or only) directory.
4732         &require_file ($GNU, 'INSTALL', 'NEWS', 'README', 'COPYING',
4733                        'AUTHORS', 'ChangeLog');
4734     }
4735
4736     if ($strictness >= $GNU
4737         && defined $options{'no-installman'})
4738     {
4739         &am_line_error ('AUTOMAKE_OPTIONS',
4740                         "option `no-installman' disallowed by GNU standards");
4741     }
4742
4743     if ($strictness >= $GNU
4744         && defined $options{'no-installinfo'})
4745     {
4746         &am_line_error ('AUTOMAKE_OPTIONS',
4747                         "option `no-installinfo' disallowed by GNU standards");
4748     }
4749 }
4750
4751 # Do any extra checking for GNITS standards.
4752 sub check_gnits_standards
4753 {
4754     if ($relative_dir eq '.')
4755     {
4756         # In top level (or only) directory.
4757         &require_file ($GNITS, 'THANKS');
4758     }
4759 }
4760
4761 ################################################################
4762 #
4763 # Functions to handle files of each language.
4764
4765 # Each `lang_X_rewrite($DIRECTORY, $BASE, $EXT)' function follows a
4766 # simple formula: Return value is $LANG_SUBDIR if the resulting object
4767 # file should be in a subdir if the source file is, $LANG_PROCESS if
4768 # file is to be dealt with, $LANG_IGNORE otherwise.
4769
4770 # Much of the actual processing is handled in
4771 # handle_single_transform_list.  These functions exist so that
4772 # auxiliary information can be recorded for a later cleanup pass.
4773 # Note that the calls to these functions are computed, so don't bother
4774 # searching for their precise names in the source.
4775
4776 # This is just a convenience function that can be used to determine
4777 # when a subdir object should be used.
4778 sub lang_sub_obj
4779 {
4780     return defined $options{'subdir-objects'} ? $LANG_SUBDIR : $LANG_PROCESS;
4781 }
4782
4783 # Rewrite a single C source file.
4784 sub lang_c_rewrite
4785 {
4786     my ($directory, $base, $ext) = @_;
4787
4788     if (defined $options{'ansi2knr'} && $base =~ /_$/)
4789     {
4790         # FIXME: include line number in error.
4791         &am_error ("C source file `$base.c' would be deleted by ansi2knr rules");
4792     }
4793
4794     my $r = $LANG_PROCESS;
4795     if (defined $options{'subdir-objects'})
4796     {
4797         $r = $LANG_SUBDIR;
4798         $base = $directory . '/' . $base;
4799
4800         if (! $seen_cc_c_o)
4801         {
4802             # Only give error once.
4803             $seen_cc_c_o = 1;
4804             # FIXME: line number.
4805             &am_error ("C objects in subdir but `AM_PROG_CC_C_O' not in `$configure_ac'");
4806         }
4807
4808         &require_file ($FOREIGN, 'compile')
4809             if $relative_dir eq '.';
4810     }
4811
4812     $de_ansi_files{$base} = 1;
4813     return $r;
4814 }
4815
4816 # Rewrite a single C++ source file.
4817 sub lang_cxx_rewrite
4818 {
4819     return &lang_sub_obj;
4820 }
4821
4822 # Rewrite a single header file.
4823 sub lang_header_rewrite
4824 {
4825     # Header files are simply ignored.
4826     return $LANG_IGNORE;
4827 }
4828
4829 # Rewrite a single yacc file.
4830 sub lang_yacc_rewrite
4831 {
4832     my ($directory, $base, $ext) = @_;
4833
4834     my $r = &lang_c_rewrite ($directory, $base, $ext);
4835     my $pfx = '';
4836     if ($r == $LANG_SUBDIR)
4837     {
4838         $pfx = $directory . '/';
4839     }
4840     $yacc_sources{$pfx . $base . '.' . $ext} = 1;
4841     $ext =~ tr/y/c/;
4842     &saw_extension ('c');
4843     # FIXME: nodist.
4844     &push_dist_common ($pfx . $base . '.' . $ext);
4845     return $r;
4846 }
4847
4848 # Rewrite a single yacc++ file.
4849 sub lang_yaccxx_rewrite
4850 {
4851     my ($directory, $base, $ext) = @_;
4852
4853     my $r = $LANG_PROCESS;
4854     my $pfx = '';
4855     if (defined $options{'subdir-objects'})
4856     {
4857         $pfx = $directory . '/';
4858         $r = $LANG_SUBDIR;
4859     }
4860     $yacc_sources{$pfx . $base . '.' . $ext} = 1;
4861     $ext =~ tr/y/c/;
4862     &saw_extension ($ext);
4863     # FIXME: nodist.
4864     &push_dist_common ($pfx . $base . '.' . $ext);
4865     return $r;
4866 }
4867
4868 # Rewrite a single lex file.
4869 sub lang_lex_rewrite
4870 {
4871     my ($directory, $base, $ext) = @_;
4872
4873     my $r = &lang_c_rewrite ($directory, $base, $ext);
4874     my $pfx = '';
4875     if ($r == $LANG_SUBDIR)
4876     {
4877         $pfx = $directory . '/';
4878     }
4879     $lex_sources{$pfx . $base . '.' . $ext} = 1;
4880     $ext =~ tr/l/c/;
4881     &saw_extension ('c');
4882     # FIXME: nodist.
4883     &push_dist_common ($pfx . $base . '.' . $ext);
4884     return $r;
4885 }
4886
4887 # Rewrite a single lex++ file.
4888 sub lang_lexxx_rewrite
4889 {
4890     my ($directory, $base, $ext) = @_;
4891
4892     my $r = $LANG_PROCESS;
4893     my $pfx = '';
4894     if (defined $options{'subdir-objects'})
4895     {
4896         $pfx = $directory . '/';
4897         $r = $LANG_SUBDIR;
4898     }
4899     $lex_sources{$pfx . $base . '.' . $ext} = 1;
4900     $ext =~ tr/l/c/;
4901     &saw_extension ($ext);
4902     # FIXME: nodist.
4903     &push_dist_common ($pfx . $base . '.' . $ext);
4904     return $r;
4905 }
4906
4907 # Rewrite a single assembly file.
4908 sub lang_asm_rewrite
4909 {
4910     return &lang_sub_obj;
4911 }
4912
4913 # Rewrite a single Fortran 77 file.
4914 sub lang_f77_rewrite
4915 {
4916     return $LANG_PROCESS;
4917 }
4918
4919 # Rewrite a single preprocessed Fortran 77 file.
4920 sub lang_ppf77_rewrite
4921 {
4922     return $LANG_PROCESS;
4923 }
4924
4925 # Rewrite a single ratfor file.
4926 sub lang_ratfor_rewrite
4927 {
4928     return $LANG_PROCESS;
4929 }
4930
4931 # Rewrite a single Objective C file.
4932 sub lang_objc_rewrite
4933 {
4934     return &lang_sub_obj;
4935 }
4936
4937 # Rewrite a single Java file.
4938 sub lang_java_rewrite
4939 {
4940     return $LANG_SUBDIR;
4941 }
4942
4943 # The lang_X_finish functions are called after all source file
4944 # processing is done.  Each should handle defining rules for the
4945 # language, etc.  A finish function is only called if a source file of
4946 # the appropriate type has been seen.
4947
4948 sub lang_c_finish
4949 {
4950     # Push all libobjs files onto de_ansi_files.  We actually only
4951     # push files which exist in the current directory, and which are
4952     # genuine source files.
4953     foreach my $file (keys %libsources)
4954     {
4955         if ($file =~ /^(.*)\.[cly]$/ && -f "$relative_dir/$file")
4956         {
4957             $de_ansi_files{$1} = 1;
4958         }
4959     }
4960
4961     if (defined $options{'ansi2knr'} && keys %de_ansi_files)
4962     {
4963         # Make all _.c files depend on their corresponding .c files.
4964         my @objects;
4965         foreach my $base (sort keys %de_ansi_files)
4966         {
4967             # Each _.c file must depend on ansi2knr; otherwise it
4968             # might be used in a parallel build before it is built.
4969             # We need to support files in the srcdir and in the build
4970             # dir (because these files might be auto-generated.  But
4971             # we can't use $< -- some makes only define $< during a
4972             # suffix rule.
4973             $output_rules .= ($base . "_.c: $base.c \$(ANSI2KNR)\n\t"
4974                               . '$(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) '
4975                               . '`if test -f $(srcdir)/' . $base . '.c'
4976                               . '; then echo $(srcdir)/' . $base . '.c'
4977                               . '; else echo ' . $base . '.c; fi` '
4978                               . "| sed 's/^# \\([0-9]\\)/#line \\1/' "
4979                               . '| $(ANSI2KNR) > ' . $base . "_.c\n");
4980             push (@objects, $base . '_.$(OBJEXT)');
4981             push (@objects, $base . '_.lo')
4982               if $seen_libtool;
4983         }
4984
4985         # Make all _.o (and _.lo) files depend on ansi2knr.
4986         # Use a sneaky little hack to make it print nicely.
4987         &pretty_print_rule ('', '', @objects, ':', '$(ANSI2KNR)');
4988     }
4989 }
4990
4991
4992 # This is a helper for both lex and yacc.
4993 sub yacc_lex_finish_helper
4994 {
4995     return if defined $language_scratch{'lex-yacc-done'};
4996     $language_scratch{'lex-yacc-done'} = 1;
4997
4998     # If there is more than one distinct yacc (resp lex) source file
4999     # in a given directory, then the `ylwrap' program is required to
5000     # allow parallel builds to work correctly.  FIXME: for now, no
5001     # line number.
5002     &require_config_file ($FOREIGN, 'ylwrap');
5003     if ($config_aux_dir_set_in_configure_in)
5004     {
5005         &define_variable ('YLWRAP', $config_aux_dir . "/ylwrap");
5006     }
5007     else
5008     {
5009         &define_variable ('YLWRAP', '$(srcdir)/ylwrap');
5010     }
5011 }
5012
5013 sub lang_yacc_finish
5014 {
5015     return if defined $language_scratch{'yacc-done'};
5016     $language_scratch{'yacc-done'} = 1;
5017
5018     my %seen_suffix = ();
5019     my @yacc_files = sort keys %yacc_sources;
5020     my $yacc_count = scalar (@yacc_files);
5021     foreach my $file (@yacc_files)
5022     {
5023         $file =~ /(\..*)$/;
5024         &output_yacc_build_rule ($1, $yacc_count > 1)
5025             if ! defined $seen_suffix{$1};
5026         $seen_suffix{$1} = 1;
5027
5028         $file =~ /^(.*)\.(y|yy|y\+\+|yxx|ypp)$/;
5029         my $base = $1;
5030         my $hname = 'h';                # Always use `.h' for header file.
5031         my $cname = $2;
5032         $cname =~ tr/y/c/;
5033
5034         if ((&variable_defined ('AM_YFLAGS')
5035              && &variable_value ('AM_YFLAGS') =~ /(^|\s)-d(\s|$)/)
5036             || (&variable_defined ('YFLAGS')
5037                 && &variable_value ('YFLAGS') =~ /(^|\s)-d(\s|$)/)) {
5038             # Now generate rule to make the header file.  This should only
5039             # be generated if `yacc -d' specified.
5040             $output_rules .= "${base}.${hname}: ${base}.${cname}\n";
5041
5042             # If the files are built in the build directory, then we want
5043             # to remove them with `make clean'.  If they are in srcdir
5044             # they shouldn't be touched.  However, we can't determine this
5045             # statically, and the GNU rules say that yacc/lex output files
5046             # should be removed by maintainer-clean.  So that's what we
5047             # do.
5048             push (@maintainer_clean_files, "${base}.${hname}");
5049
5050             &push_dist_common ("${base}.${hname}");
5051         }
5052         push (@maintainer_clean_files, "${base}.${cname}");
5053     }
5054     $output_rules .= "\n";
5055
5056     if (&variable_defined ('YACCFLAGS'))
5057     {
5058         &am_line_error ('YACCFLAGS',
5059                         "`YACCFLAGS' obsolete; use `YFLAGS' instead");
5060     }
5061
5062     if ($yacc_count > 1)
5063     {
5064         &yacc_lex_finish_helper;
5065     }
5066 }
5067
5068
5069 sub lang_lex_finish
5070 {
5071     return if defined $language_scratch{'lex-done'};
5072     $language_scratch{'lex-done'} = 1;
5073
5074     my %seen_suffix = ();
5075     my $lex_count = scalar (keys %lex_sources);
5076     foreach my $file (sort keys %lex_sources)
5077     {
5078         $file =~ /(\..*)$/;
5079         &output_lex_build_rule ($1, $lex_count > 1)
5080             if (! defined $seen_suffix{$1});
5081         $seen_suffix{$1} = 1;
5082
5083         # If the files are built in the build directory, then we want
5084         # to remove them with `make clean'.  If they are in srcdir
5085         # they shouldn't be touched.  However, we can't determine this
5086         # statically, and the GNU rules say that yacc/lex output files
5087         # should be removed by maintainer-clean.  So that's what we
5088         # do.
5089         $file =~ /^(.*)\.(l|ll|l\+\+|lxx|lpp)$/;
5090         my $cname;
5091         ($cname = $2) =~ tr/l/c/;
5092         push (@maintainer_clean_files, "${1}.${cname}");
5093     }
5094
5095     if (! $seen_decl_yytext)
5096     {
5097         &am_error ("lex source seen but `AC_DECL_YYTEXT' not in `$configure_ac'");
5098     }
5099
5100     if ($lex_count > 1)
5101     {
5102         &yacc_lex_finish_helper;
5103     }
5104 }
5105
5106
5107 # Given a hash table of linker names, pick the name that has the most
5108 # precedence.  This is lame, but something has to have global
5109 # knowledge in order to eliminate the conflict.  Add more linkers as
5110 # required.
5111 sub resolve_linker
5112 {
5113     my (%linkers) = @_;
5114
5115     return 'GCJLINK'
5116         if defined $linkers{'GCJLINK'};
5117     return 'CXXLINK'
5118         if defined $linkers{'CXXLINK'};
5119     return 'F77LINK'
5120         if defined $linkers{'F77LINK'};
5121     return 'OBJCLINK'
5122         if defined $linkers{'OBJCLINK'};
5123     return 'LINK';
5124 }
5125
5126 # Called to indicate that an extension was used.
5127 sub saw_extension
5128 {
5129     my ($ext) = @_;
5130     $extension_seen{$ext} = 1;
5131 }
5132
5133 # Called to ask whether source files have been seen . If HEADERS is 1,
5134 # headers can be included.
5135 sub saw_sources_p
5136 {
5137     my ($headers) = @_;
5138
5139     if ($headers)
5140     {
5141         $headers = 0;
5142     }
5143     else
5144     {
5145         $headers = grep ($extension_seen{$_},
5146                          @{$languages{'header'}->extensions});
5147     }
5148
5149     return scalar keys %extension_seen > $headers;
5150 }
5151
5152
5153 # register_language (%ATTRIBUTE)
5154 # ------------------------------
5155 # Register a single language.
5156 # Each %ATTRIBUTE is of the form ATTRIBUTE => VALUE.
5157 sub register_language (%)
5158 {
5159     my (%option) = @_;
5160
5161     # Set the defaults.
5162     $option{'ansi'} = 0
5163       unless defined $option{'ansi'};
5164     $option{'autodep'} = 'no'
5165       unless defined $option{'autodep'};
5166     $option{'derived_autodep'} = 'no'
5167       unless defined $option{'derived_autodep'};
5168     $option{'linker'} = ''
5169       unless defined $option{'linker'};
5170
5171     my $lang = new Language (%option);
5172
5173     # Fill indexes.
5174     grep ($extension_map{$_} = $lang->name, @{$lang->extensions});
5175     $languages{$lang->name} = $lang;
5176 }
5177
5178 # This function is used to find a path from a user-specified suffix to
5179 # `o' or to some other suffix we recognize internally, eg `cc'.
5180 sub derive_suffix
5181 {
5182     my ($source_ext) = @_;
5183
5184     # FIXME: hard-coding `o' is a mistake.  Doing something
5185     # intelligent is harder.
5186     while ($extension_map{$source_ext} eq ''
5187            && $source_ext ne 'o'
5188            && defined $suffix_rules{$source_ext})
5189     {
5190         $source_ext = $suffix_rules{$source_ext};
5191     }
5192
5193     return $source_ext;
5194 }
5195
5196
5197 ################################################################
5198
5199 # Pretty-print something.  HEAD is what should be printed at the
5200 # beginning of the first line, FILL is what should be printed at the
5201 # beginning of every subsequent line.
5202 sub pretty_print_internal
5203 {
5204     my ($head, $fill, @values) = @_;
5205
5206     my $column = length ($head);
5207     my $result = $head;
5208
5209     # Fill length is number of characters.  However, each Tab
5210     # character counts for eight.  So we count the number of Tabs and
5211     # multiply by 7.
5212     my $fill_length = length ($fill);
5213     $fill_length += 7 * ($fill =~ tr/\t/\t/d);
5214
5215     foreach (@values)
5216     {
5217         # "71" because we also print a space.
5218         if ($column + length ($_) > 71)
5219         {
5220             $result .= " \\\n" . $fill;
5221             $column = $fill_length;
5222         }
5223         $result .= ' ' if $result =~ /\S\z/;
5224         $result .= $_;
5225         $column += length ($_) + 1;
5226     }
5227
5228     $result .= "\n";
5229     return $result;
5230 }
5231
5232 # Pretty-print something and append to output_vars.
5233 sub pretty_print
5234 {
5235     $output_vars .= &pretty_print_internal (@_);
5236 }
5237
5238 # Pretty-print something and append to output_rules.
5239 sub pretty_print_rule
5240 {
5241     $output_rules .= &pretty_print_internal (@_);
5242 }
5243
5244
5245 ################################################################
5246
5247
5248 # $STRING
5249 # &conditional_string(@COND-STACK)
5250 # --------------------------------
5251 # Build a string de
5252 sub conditional_string
5253 {
5254   my (@stack) = @_;
5255
5256   if (grep (/^FALSE$/, @stack))
5257     {
5258       return 'FALSE';
5259     }
5260   else
5261     {
5262       return join (' ', uniq sort grep (!/^TRUE$/, @stack));
5263     }
5264 }
5265
5266
5267 # $BOOLEAN
5268 # &conditional_true_when ($COND, $WHEN)
5269 # -------------------------------------
5270 # See if a conditional is true.  Both arguments are conditional
5271 # strings.  This returns true if the first conditional is true when
5272 # the second conditional is true.
5273 # For instance with $COND = `BAR FOO', and $WHEN = `BAR BAZ FOO',
5274 # obviously return 1, and 0 when, for instance, $WHEN = `FOO'.
5275 sub conditional_true_when ($$)
5276 {
5277     my ($cond, $when) = @_;
5278
5279     # Make a hash holding all the values from $WHEN.
5280     my %cond_vals = map { $_ => 1 } split (' ', $when);
5281
5282     # Check each component of $cond, which looks `COND1 COND2'.
5283     foreach my $comp (split (' ', $cond))
5284     {
5285         # TRUE is always true.
5286         next if $comp eq 'TRUE';
5287         return 0 if ! defined $cond_vals{$comp};
5288     }
5289
5290     return 1;
5291 }
5292
5293
5294 # $BOOLEAN
5295 # &conditionals_true_when (@CONDS, @WHENS)
5296 # ----------------------------------------
5297 # Same as above, but true if all the @CONDS are true when *ALL*
5298 # the @WHENS are sufficient.
5299 #
5300 # If there are no @CONDS, then return true, of course. *BUT*, even if there
5301 # are @CONDS but @WHENS is empty, return true.  This is counter intuitive,
5302 # and against all the rules of logic, but is needed by the current code.
5303 # FIXME: Do something saner when the logic of conditionals is understood.
5304 sub conditionals_true_when (@@)
5305 {
5306     my (@conds, @whens) = @_;
5307
5308     foreach my $cond (@conds)
5309     {
5310       foreach my $when (@whens)
5311       {
5312         return 0
5313           unless conditional_true_when ($cond, $when);
5314       }
5315     }
5316
5317     return 1;
5318 }
5319
5320
5321 # $NEGATION
5322 # condition_negate ($COND)
5323 # ------------------------
5324 sub condition_negate ($)
5325 {
5326     my ($cond) = @_;
5327
5328     $cond =~ s/TRUE$/TRUEO/;
5329     $cond =~ s/FALSE$/TRUE/;
5330     $cond =~ s/TRUEO$/FALSE/;
5331
5332     return $cond;
5333 }
5334
5335
5336 # Compare condition names.
5337 # Issue them in alphabetical order, foo_TRUE before foo_FALSE.
5338 sub by_condition
5339 {
5340     # Be careful we might be comparing `' or `#'.
5341     $a =~ /^(.*)_(TRUE|FALSE)$/;
5342     my ($aname, $abool) = ($1 || '', $2 || '');
5343     $b =~ /^(.*)_(TRUE|FALSE)$/;
5344     my ($bname, $bbool) = ($1 || '', $2 || '');
5345     return ($aname cmp $bname
5346             # Don't bother with IFs, given that TRUE is after FALSE
5347             # just cmp in the reverse order.
5348             || $bbool cmp $abool
5349             # Just in case...
5350             || $a cmp $b);
5351 }
5352
5353
5354 # &make_condition (@CONDITIONS)
5355 # -----------------------------
5356 # Transform a list of conditions (themselves can be an internal list
5357 # of conditions, e.g., @CONDITIONS = ('cond1 cond2', 'cond3')) into a
5358 # Make conditional (a pattern for AC_SUBST).
5359 # Correctly returns the empty string when there are no conditions.
5360 sub make_condition
5361 {
5362     my $res = conditional_string (@_);
5363
5364     # There are no conditions.
5365     if ($res eq '')
5366       {
5367         # Nothing to do.
5368       }
5369     # It's impossible.
5370     elsif ($res eq 'FALSE')
5371       {
5372         $res = '#';
5373       }
5374     # Build it.
5375     else
5376       {
5377         $res = '@' . $res . '@';
5378         $res =~ s/ /@@/g;
5379       }
5380
5381     return $res;
5382 }
5383
5384
5385
5386 ## ------------------------------ ##
5387 ## Handling the condition stack.  ##
5388 ## ------------------------------ ##
5389
5390
5391 # $COND_STRING
5392 # cond_stack_if ($NEGATE, $COND, $WHERE)
5393 # --------------------------------------
5394 sub cond_stack_if ($$$)
5395 {
5396   my ($negate, $cond, $where) = @_;
5397
5398   &am_file_error ($where, "$cond does not appear in AM_CONDITIONAL")
5399     if ! $configure_cond{$cond} && $cond !~ /^TRUE|FALSE$/;
5400
5401   $cond = "${cond}_TRUE"
5402     unless $cond =~ /^TRUE|FALSE$/;
5403   $cond = condition_negate ($cond)
5404     if $negate;
5405
5406   push (@cond_stack, $cond);
5407
5408   return conditional_string (@cond_stack);
5409 }
5410
5411
5412 # $COND_STRING
5413 # cond_stack_else ($NEGATE, $COND, $WHERE)
5414 # ----------------------------------------
5415 sub cond_stack_else ($$$)
5416 {
5417   my ($negate, $cond, $where) = @_;
5418
5419   if (! @cond_stack)
5420     {
5421       &am_file_error ($where, "else without if");
5422       return;
5423     }
5424
5425   $cond_stack[$#cond_stack] = condition_negate ($cond_stack[$#cond_stack]);
5426
5427   # If $COND is given, check against it.
5428   if (defined $cond)
5429     {
5430       $cond = "${cond}_TRUE"
5431         unless $cond =~ /^TRUE|FALSE$/;
5432       $cond = condition_negate ($cond)
5433         if $negate;
5434
5435       &am_file_error ($where,
5436                       "else reminder ($negate$cond) incompatible with "
5437                       . "current conditional: $cond_stack[$#cond_stack]")
5438         if $cond_stack[$#cond_stack] ne $cond;
5439     }
5440
5441   return conditional_string (@cond_stack);
5442 }
5443
5444
5445 # $COND_STRING
5446 # cond_stack_endif ($NEGATE, $COND, $WHERE)
5447 # -----------------------------------------
5448 sub cond_stack_endif ($$$)
5449 {
5450   my ($negate, $cond, $where) = @_;
5451   my $old_cond;
5452
5453   if (! @cond_stack)
5454     {
5455       &am_file_error ($where, "endif without if: $negate$cond");
5456       return;
5457     }
5458
5459
5460   # If $COND is given, check against it.
5461   if (defined $cond)
5462     {
5463       $cond = "${cond}_TRUE"
5464         unless $cond =~ /^TRUE|FALSE$/;
5465       $cond = condition_negate ($cond)
5466         if $negate;
5467
5468       &am_file_error ($where,
5469                       "endif reminder ($negate$cond) incompatible with "
5470                       . "current conditional: $cond_stack[$#cond_stack]")
5471         if $cond_stack[$#cond_stack] ne $cond;
5472     }
5473
5474   pop @cond_stack;
5475
5476   return conditional_string (@cond_stack);
5477 }
5478
5479
5480
5481
5482
5483 ## ------------------------ ##
5484 ## Handling the variables.  ##
5485 ## ------------------------ ##
5486
5487
5488 # check_ambiguous_conditional ($VAR, $COND)
5489 # -----------------------------------------
5490 # Check for an ambiguous conditional.  This is called when a variable
5491 # is being defined conditionally.  If we already know about a
5492 # definition that is true under the same conditions, then we have an
5493 # ambiguity.
5494 sub check_ambiguous_conditional ($$)
5495 {
5496     my ($var, $cond) = @_;
5497     foreach my $vcond (keys %{$var_value{$var}})
5498     {
5499        my $message;
5500        if ($vcond eq $cond)
5501        {
5502            $message = "$var multiply defined in condition $cond";
5503        }
5504        elsif (&conditional_true_when ($vcond, $cond))
5505        {
5506          $message = ("$var was already defined in condition $vcond, "
5507                      . "which implies condition $cond");
5508        }
5509        elsif (&conditional_true_when ($cond, $vcond))
5510        {
5511            $message = ("$var was already defined in condition $vcond, "
5512                        . "which is implied by condition $cond");
5513        }
5514        if ($message)
5515        {
5516            &am_line_error ($var, $message);
5517            macro_dump ($var);
5518        }
5519    }
5520 }
5521
5522
5523 # &macro_define($VAR, $VAR_IS_AM, $TYPE, $COND, $VALUE, $WHERE)
5524 # -------------------------------------------------------------
5525 # The $VAR can go from Automake to user, but not the converse.
5526 sub macro_define ($$$$$$)
5527 {
5528   my ($var, $var_is_am, $type, $cond, $value, $where) = @_;
5529
5530   am_file_error ($where, "bad macro name `$var'")
5531     if $var !~ /$MACRO_PATTERN/o;
5532
5533   $cond ||= 'TRUE';
5534
5535   # An Automake variable must be consistently defined with the same
5536   # sign by Automake.  A user variable must be set by either `=' or
5537   # `:=', and later promoted to `+='.
5538   if ($var_is_am)
5539     {
5540       if (defined $var_type{$var} && $var_type{$var} ne $type)
5541         {
5542           am_line_error ($var,
5543                          ("$var was set with `$var_type{$var}=' "
5544                           . "and is now set with `$type='"));
5545         }
5546     }
5547   else
5548     {
5549       if (!defined $var_type{$var} && $type eq '+')
5550         {
5551           am_line_error ($var, "$var must be set with `=' before using `+='");
5552         }
5553     }
5554   $var_type{$var} = $type;
5555
5556   # When adding, since we rewrite, don't try to preserve the
5557   # Automake continuation backslashes.
5558   $value =~ s/\\$//mg
5559     if $type eq '+' && $var_is_am;
5560
5561   # Differentiate the first assignment (including with `+=').
5562   if ($type eq '+' && defined $var_value{$var}{$cond})
5563     {
5564       if (substr ($var_value{$var}{$cond}, -1) eq "\n")
5565         {
5566           # Insert a backslash before a trailing newline.
5567           $var_value{$var}{$cond} =
5568             substr ($var_value{$var}{$cond}, 0, -1) . "\\\n";
5569         }
5570       elsif ($var_value{$var}{$cond})
5571         {
5572           # Insert a separator.
5573           $var_value{$var}{$cond} .= ' ';
5574         }
5575        $var_value{$var}{$cond} .= $value;
5576     }
5577   else
5578     {
5579       # The first assignment to a macro sets the line number.  Ideally I
5580       # suppose we would associate line numbers with random bits of text.
5581       # FIXME: We sometimes redefine some variables, but we want to keep
5582       # the original location.  More subs are needed to handle
5583       # properly variables.  Once this done, remove this hack.
5584       $var_line{$var} = $where
5585         unless defined $var_line{$var};
5586
5587       # If Automake tries to override a value specified by the user,
5588       # just don't let it do.
5589       if (defined $var_value{$var}{$cond} && !$var_is_am{$var} && $var_is_am)
5590         {
5591           if ($verbose)
5592             {
5593               print STDERR "$me: refusing to override the user definition of:\n";
5594               macro_dump ($var);
5595               print STDERR "$me: with `$cond' => `$value'\n";
5596             }
5597         }
5598       else
5599         {
5600           # There must be no previous value unless the user is redefining
5601           # an Automake variable or an AC_SUBST variable.
5602           check_ambiguous_conditional ($var, $cond)
5603             unless ($var_is_am{$var} && !$var_is_am
5604                     || exists $configure_vars{$var});
5605
5606           $var_value{$var}{$cond} = $value;
5607         }
5608     }
5609
5610   # An Automake variable can be given to the user, but not the converse.
5611   if (! defined $var_is_am{$var} || !$var_is_am)
5612     {
5613       $var_is_am{$var} = $var_is_am;
5614     }
5615 }
5616
5617
5618 # &variable_delete ($VAR, [@CONDS])
5619 # ---------------------------------
5620 # Forget about $VAR under the conditions @CONDS, or completely if
5621 # @CONDS is empty.
5622 sub variable_delete ($@)
5623 {
5624   my ($var, @conds) = @_;
5625
5626   if (!@conds)
5627     {
5628       delete $var_value{$var};
5629       delete $var_line{$var};
5630       delete $var_is_am{$var};
5631       delete $var_comment{$var};
5632       delete $var_type{$var};
5633     }
5634   else
5635     {
5636       foreach my $cond (@conds)
5637         {
5638           delete $var_value{$var}{$cond};
5639         }
5640     }
5641 }
5642
5643
5644 # &macro_dump ($VAR)
5645 # ------------------
5646 sub macro_dump ($)
5647 {
5648   my ($var) = @_;
5649
5650   if (!exists $var_value{$var})
5651     {
5652       print STDERR "  $var does not exist\n";
5653     }
5654   else
5655     {
5656       my $var_is_am = $var_is_am{$var} ? "Automake" : "User";
5657       my $where = (defined $var_line{$var}
5658                    ? $var_line{$var} : "undefined");
5659       print STDERR "$var_comment{$var}"
5660         if defined $var_comment{$var};
5661       print STDERR "  $var ($var_is_am, where = $where) $var_type{$var}=\n";
5662       print STDERR "  {\n";
5663       foreach my $vcond (sort by_condition keys %{$var_value{$var}})
5664         {
5665           print STDERR "    $vcond => $var_value{$var}{$vcond}\n";
5666         }
5667       print STDERR "  }\n";
5668     }
5669 }
5670
5671
5672 # &macros_dump ()
5673 # ---------------
5674 sub macros_dump ()
5675 {
5676   my ($var) = @_;
5677
5678   print STDERR "%var_value =\n";
5679   print STDERR "{\n";
5680   foreach my $var (sort (keys %var_value))
5681     {
5682       macro_dump ($var);
5683     }
5684   print STDERR "}\n";
5685 }
5686
5687
5688 # $BOOLEAN
5689 # &variable_defined ($VAR, [$COND])
5690 # ---------------------------------
5691 # See if a variable exists.  $VAR is the variable name, and $COND is
5692 # the condition which we should check.  If no condition is given, we
5693 # currently return true if the variable is defined under any
5694 # condition.
5695 sub variable_defined ($$)
5696 {
5697     my ($var, $cond) = @_;
5698
5699     # Unfortunately we can't just check for $var_value{VAR}{COND}
5700     # as this would make perl create $condition{VAR}, which we
5701     # don't want.
5702     if (!exists $var_value{$var})
5703       {
5704         if (defined $targets{$var})
5705           {
5706             &am_line_error ($var, "`$var' is a target; expected a variable")
5707           }
5708         # The variable is not defined
5709         return 0;
5710       }
5711
5712     if ($cond && !exists $var_value{$var}{$cond})
5713       {
5714         # The variable is not defined for the given condition.
5715         return 0;
5716       }
5717
5718     # Even a var_value examination is good enough for us.  FIXME:
5719     # really should maintain examined status on a per-condition basis.
5720     $content_seen{$var} = 1;
5721     return 1;
5722 }
5723
5724 # Mark a variable as examined.
5725 sub examine_variable
5726 {
5727     my ($var) = @_;
5728     &variable_defined ($var);
5729 }
5730
5731 # Return the set of conditions for which a variable is defined.
5732
5733 # If the variable is not defined conditionally, and is not defined in
5734 # terms of any variables which are defined conditionally, then this
5735 # returns the empty list.
5736
5737 # If the variable is defined conditionally, but is not defined in
5738 # terms of any variables which are defined conditionally, then this
5739 # returns the list of conditions for which the variable is defined.
5740
5741 # If the variable is defined in terms of any variables which are
5742 # defined conditionally, then this returns a full set of permutations
5743 # of the subvariable conditions.  For example, if the variable is
5744 # defined in terms of a variable which is defined for COND_TRUE,
5745 # then this returns both COND_TRUE and COND_FALSE.  This is
5746 # because we will need to define the variable under both conditions.
5747
5748 sub variable_conditions ($)
5749 {
5750     my ($var) = @_;
5751     my %uniqify;
5752     my @uniq_list;
5753
5754     %vars_scanned = ();
5755     foreach my $cond (&variable_conditions_sub ($var, '', ()))
5756     {
5757         next
5758           if $cond eq 'FALSE';
5759         $uniqify{$cond} = 1;
5760     }
5761
5762     @uniq_list = sort by_condition keys %uniqify;
5763     # Note we cannot just do `return sort keys %uniqify', because this
5764     # function is sometimes used in a scalar context.
5765     return @uniq_list;
5766 }
5767
5768
5769 # $BOOLEAN
5770 # &variable_conditionally_defined ($VAR)
5771 # --------------------------------------
5772 sub variable_conditionally_defined ($)
5773 {
5774     my ($var) = @_;
5775     foreach my $cond (variable_conditions ($var))
5776       {
5777         return 1
5778           unless $cond =~ /^TRUE|FALSE$/;
5779       }
5780     return 0;
5781 }
5782
5783
5784
5785 # &variable_conditions_sub ($VAR, $PARENT, @PARENT_CONDS)
5786 # -------------------------------------------------------
5787 # A subroutine of variable_conditions.  We only return conditions
5788 # which are true for all the conditions in @PARENT_CONDS.
5789 sub variable_conditions_sub
5790 {
5791     my ($var, $parent, @parent_conds) = @_;
5792     my @new_conds = ();
5793
5794     if (defined $vars_scanned{$var})
5795     {
5796         &am_line_error ($parent, "variable `$var' recursively defined");
5797         return ();
5798     }
5799     $vars_scanned{$var} = 1;
5800
5801     my @this_conds = ();
5802     foreach my $vcond (keys %{$var_value{$var}})
5803     {
5804         next
5805           if ! conditionals_true_when ((@parent_conds), ($vcond));
5806
5807         push (@this_conds, $vcond);
5808
5809         push (@parent_conds, $vcond);
5810         my @subvar_conds = ();
5811         foreach (split (' ', $var_value{$var}{$vcond}))
5812         {
5813             # If a comment seen, just leave.
5814             last if /^#/;
5815
5816             # Handle variable substitutions.
5817             if (/^\$\{(.*)\}$/ || /^\$\((.*)\)$/)
5818             {
5819                 push (@subvar_conds,
5820                       &variable_conditions_sub ($1, $var, @parent_conds));
5821             }
5822         }
5823         pop (@parent_conds);
5824
5825         # If there are no conditional subvariables, then we want to
5826         # return this condition.  Otherwise, we want to return the
5827         # permutations of the subvariables.
5828         if (! @subvar_conds)
5829         {
5830             push (@new_conds, $vcond);
5831         }
5832         else
5833         {
5834             push (@new_conds, &variable_conditions_reduce (@subvar_conds));
5835         }
5836     }
5837
5838     # Unset our entry in vars_scanned.  We only care about recursive
5839     # definitions.
5840     delete $vars_scanned{$var};
5841
5842     # If there are no parents, then this call is the top level call.
5843     if (! $parent)
5844       {
5845         # Now we want to return all permutations of the subvariable
5846         # conditions.
5847         my %allconds = ();
5848         foreach my $item (@new_conds)
5849         {
5850             foreach (split (' ', $item))
5851             {
5852                 s/^(.*)_(TRUE|FALSE)$/$1_TRUE/;
5853                 $allconds{$_} = 1;
5854             }
5855         }
5856         return &variable_conditions_permutations (sort keys %allconds);
5857       }
5858
5859     # If we are being called on behalf of another variable, we need to
5860     # return all possible permutations of the conditions.  We have
5861     # already handled everything in @this_conds along with their
5862     # subvariables.  We now need to add any permutations that are not
5863     # in @this_conds.
5864     foreach my $this_cond (@this_conds)
5865     {
5866         my @perms =
5867             &variable_conditions_permutations (split(' ', $this_cond));
5868         foreach my $perm (@perms)
5869         {
5870             my $ok = 1;
5871             foreach my $scan (@this_conds)
5872             {
5873                 if (&conditional_true_when ($perm, $scan)
5874                     || &conditional_true_when ($scan, $perm))
5875                 {
5876                     $ok = 0;
5877                     last;
5878                 }
5879             }
5880             next if ! $ok;
5881
5882             next
5883               if ! conditionals_true_when ((@parent_conds), ($perm));
5884
5885             # This permutation was not already handled, and is valid
5886             # for the parents.
5887             push (@new_conds, $perm);
5888         }
5889     }
5890
5891     return @new_conds;
5892 }
5893
5894
5895 # Filter a list of conditionals so that only the exclusive ones are
5896 # retained.  For example, if both `COND1_TRUE COND2_TRUE' and
5897 # `COND1_TRUE' are in the list, discard the latter.
5898 sub variable_conditions_reduce
5899 {
5900     my (@conds) = @_;
5901     my @ret = ();
5902     foreach my $cond (@conds)
5903     {
5904         # FALSE is absorbent.
5905         if ($cond eq 'FALSE')
5906           {
5907             return ('FALSE');
5908           }
5909         elsif (conditionals_true_when (($cond), (@ret)))
5910           {
5911             push (@ret, $cond);
5912           }
5913     }
5914
5915     return @ret;
5916 }
5917
5918 # Return a list of permutations of a conditional string.
5919 sub variable_conditions_permutations
5920 {
5921     my (@comps) = @_;
5922     return ()
5923         if ! @comps;
5924     my $comp = shift (@comps);
5925     return &variable_conditions_permutations (@comps)
5926         if $comp eq '';
5927     my $neg = condition_negate ($comp);
5928
5929     my @ret;
5930     foreach my $sub (&variable_conditions_permutations (@comps))
5931     {
5932         push (@ret, "$comp $sub");
5933         push (@ret, "$neg $sub");
5934     }
5935     if (! @ret)
5936     {
5937         push (@ret, $comp);
5938         push (@ret, $neg);
5939     }
5940     return @ret;
5941 }
5942
5943
5944 # $BOOL
5945 # &check_variable_defined_unconditionally($VAR, $PARENT)
5946 # ------------------------------------------------------
5947 # Warn if a variable is conditionally defined.  This is called if we
5948 # are using the value of a variable.
5949 sub check_variable_defined_unconditionally ($$)
5950 {
5951     my ($var, $parent) = @_;
5952     foreach my $cond (keys %{$var_value{$var}})
5953     {
5954         next
5955           if $cond =~ /^TRUE|FALSE$/;
5956
5957         if ($parent)
5958         {
5959             &am_line_error ($parent,
5960                             "warning: automake does not support conditional definition of $var in $parent");
5961         }
5962         else
5963         {
5964             &am_line_error ($parent,
5965                             "warning: automake does not support $var being defined conditionally");
5966             macro_dump ($var);
5967
5968         }
5969     }
5970 }
5971
5972
5973 # Get the TRUE value of a variable, warn if the variable is
5974 # conditionally defined.
5975 sub variable_value
5976 {
5977     my ($var) = @_;
5978     &check_variable_defined_unconditionally ($var);
5979     return $var_value{$var}{'TRUE'};
5980 }
5981
5982
5983 # @VALUES
5984 # &value_to_list ($VAR, $VAL, $COND)
5985 # ----------------------------------
5986 # Convert a variable value to a list, split as whitespace.  This will
5987 # recursively follow $(...) and ${...} inclusions.  It preserves @...@
5988 # substitutions.
5989 #
5990 # If COND is 'all', then all values under all conditions should be
5991 # returned; if COND is a particular condition (all conditions are
5992 # surrounded by @...@) then only the value for that condition should
5993 # be returned; otherwise, warn if VAR is conditionally defined.
5994 # SCANNED is a global hash listing whose keys are all the variables
5995 # already scanned; it is an error to rescan a variable.
5996 sub value_to_list
5997 {
5998     my ($var, $val, $cond) = @_;
5999     my @result;
6000
6001     # Strip backslashes
6002     $val =~ s/\\(\n|$)/ /g;
6003
6004     foreach (split (' ', $val))
6005     {
6006         # If a comment seen, just leave.
6007         last if /^#/;
6008
6009         # Handle variable substitutions.
6010         if (/^\$\{([^}]*)\}$/ || /^\$\(([^)]*)\)$/)
6011         {
6012             my $varname = $1;
6013
6014             # If the user uses a losing variable name, just ignore it.
6015             # This isn't ideal, but people have requested it.
6016             next if ($varname =~ /\@.*\@/);
6017
6018             my ($from, $to);
6019             my @temp_list;
6020             if ($varname =~ /^([^:]*):([^=]*)=(.*)$/)
6021             {
6022                 $varname = $1;
6023                 $to = $3;
6024                 ($from = $2) =~ s/(\W)/\\$1/g;
6025             }
6026
6027             # Find the value.
6028             @temp_list = &variable_value_as_list_worker ($1, $cond, $var);
6029
6030             # Now rewrite the value if appropriate.
6031             if ($from)
6032             {
6033                 grep (s/$from$/$to/, @temp_list);
6034             }
6035
6036             push (@result, @temp_list);
6037         }
6038         else
6039         {
6040             push (@result, $_);
6041         }
6042     }
6043
6044     return @result;
6045 }
6046
6047 # Return contents of variable as list, split as whitespace.  This will
6048 # recursively follow $(...) and ${...} inclusions.  It preserves @...@
6049 # substitutions.  If COND is 'all', then all values under all
6050 # conditions should be returned; if COND is a particular condition
6051 # (all conditions are surrounded by @...@) then only the value for
6052 # that condition should be returned; otherwise, warn if VAR is
6053 # conditionally defined.  If PARENT is specified, it is the name of
6054 # the including variable; this is only used for error reports.
6055 sub variable_value_as_list_worker
6056 {
6057     my ($var, $cond, $parent) = @_;
6058     my @result = ();
6059
6060     if (! defined $var_value{$var})
6061     {
6062         if (defined $targets{$var})
6063           {
6064             &am_line_error ($var, "`$var' is a target; expected a variable");
6065           }
6066         else
6067           {
6068             &am_line_error ($parent, "variable `$var' not defined");
6069           }
6070     }
6071     elsif (defined $vars_scanned{$var})
6072     {
6073         # `vars_scanned' is a global we use to keep track of which
6074         # variables we've already examined.
6075         &am_line_error ($parent, "variable `$var' recursively defined");
6076     }
6077     elsif ($cond eq 'all')
6078     {
6079         $vars_scanned{$var} = 1;
6080         foreach my $vcond (keys %{$var_value{$var}})
6081         {
6082             my $val = $var_value{$var}{$vcond};
6083             push (@result, &value_to_list ($var, $val, $cond));
6084         }
6085     }
6086     else
6087     {
6088         $cond ||= 'TRUE';
6089         $vars_scanned{$var} = 1;
6090         my $onceflag;
6091         foreach my $vcond (keys %{$var_value{$var}})
6092         {
6093             my $val = $var_value{$var}{$vcond};
6094             if (&conditional_true_when ($vcond, $cond))
6095             {
6096                 # Warn if we have an ambiguity.  It's hard to know how
6097                 # to handle this case correctly.
6098                 &check_variable_defined_unconditionally ($var, $parent)
6099                     if $onceflag;
6100                 $onceflag = 1;
6101                 push (@result, &value_to_list ($var, $val, $cond));
6102             }
6103         }
6104     }
6105
6106     # Unset our entry in vars_scanned.  We only care about recursive
6107     # definitions.
6108     delete $vars_scanned{$var};
6109
6110     return @result;
6111 }
6112
6113
6114 # &variable_output ($VAR, [@CONDS])
6115 # ---------------------------------
6116 # Output all the values of $VAR is @COND is not specified, else only
6117 # that corresponding to @COND.
6118 sub variable_output ($@)
6119 {
6120   my ($var, @conds) = @_;
6121
6122   @conds = sort by_condition keys %{$var_value{$var}}
6123     unless @conds;
6124
6125   $output_vars .= $var_comment{$var}
6126     if defined $var_comment{$var};
6127
6128   foreach my $cond (@conds)
6129     {
6130       my $val = $var_value{$var}{$cond};
6131       my $equals = $var_type{$var} eq ':' ? ':=' : '=';
6132       my $output_var = "$var $equals $val";
6133       $output_var =~ s/^/make_condition ($cond)/meg;
6134       $output_vars .= $output_var . "\n";
6135     }
6136 }
6137
6138
6139 # &variable_pretty_output ($VAR, [@CONDS])
6140 # ----------------------------------------
6141 # Likewise, but pretty, i.e., we *split* the values at spaces.   Use only
6142 # with variables holding filenames.
6143 sub variable_pretty_output ($@)
6144 {
6145   my ($var, @conds) = @_;
6146
6147   @conds = sort by_condition keys %{$var_value{$var}}
6148     unless @conds;
6149
6150   $output_vars .= $var_comment{$var}
6151     if defined $var_comment{$var};
6152
6153   foreach my $cond (@conds)
6154     {
6155       my $val = $var_value{$var}{$cond};
6156       my $equals = $var_type{$var} eq ':' ? ':=' : '=';
6157       my $make_condition = make_condition ($cond);
6158       $output_vars .= pretty_print_internal ("$make_condition$var $equals",
6159                                              "$make_condition\t",
6160                                              split (' ' , $val));
6161     }
6162 }
6163
6164
6165 # This is just a wrapper for variable_value_as_list_worker that
6166 # initializes the global hash `vars_scanned'.  This hash is used to
6167 # avoid infinite recursion.
6168 sub variable_value_as_list
6169 {
6170     my ($var, $cond, $parent) = @_;
6171     %vars_scanned = ();
6172     return &variable_value_as_list_worker ($var, $cond, $parent);
6173 }
6174
6175
6176 # Like define_variable, but the value is a list, and the variable may
6177 # be defined conditionally.  The second argument is the conditional
6178 # under which the value should be defined; this should be the empty
6179 # string to define the variable unconditionally.  The third argument
6180 # is a list holding the values to use for the variable.  The value is
6181 # pretty printed in the output file.
6182 sub define_pretty_variable
6183 {
6184     my ($var, $cond, @value) = @_;
6185
6186     # Beware that an empty $cond has a different semantics for
6187     # macro_define and variable_pretty_output.
6188     $cond ||= 'TRUE';
6189
6190     if (! &variable_defined ($var, $cond))
6191     {
6192         macro_define ($var, 1, '', $cond, join (' ', @value), undef);
6193         variable_pretty_output ($var, $cond || 'TRUE');
6194         $content_seen{$var} = 1;
6195     }
6196 }
6197
6198
6199 # define_variable ($VAR, $VALUE)
6200 # ------------------------------
6201 # Define a new user variable VAR to VALUE, but only if not already defined.
6202 sub define_variable
6203 {
6204     my ($var, $value) = @_;
6205
6206     define_pretty_variable ($var, 'TRUE', $value);
6207 }
6208
6209
6210 # Like define_variable, but define a variable to be the configure
6211 # substitution by the same name.
6212 sub define_configure_variable
6213 {
6214     my ($var) = @_;
6215     my $value = '@' . $var . '@';
6216     &define_variable ($var, $value);
6217 }
6218
6219
6220 # define_compiler_variable ($LANG)
6221 # --------------------------------
6222 # Define a compiler variable.  We also handle defining the `LT'
6223 # version of the command when using libtool.
6224 sub define_compiler_variable ($)
6225 {
6226     my ($lang) = @_;
6227
6228     my ($var, $value) = ($lang->compiler, $lang->compile);
6229     &define_variable ($var, $value);
6230     &define_variable ("LT$var", "\$(LIBTOOL) --mode=compile $value")
6231       if $seen_libtool;
6232 }
6233
6234
6235 # define_linker_variable ($LANG)
6236 # ------------------------------
6237 # Define linker variables.
6238 sub define_linker_variable ($)
6239 {
6240     my ($lang) = @_;
6241
6242     my ($var, $value) = ($lang->lder, $lang->ld);
6243     # CCLD = $(CC).
6244     &define_variable ($lang->lder, $lang->ld);
6245     # CCLINK = $(CCLD) blah blah...
6246     &define_variable ($lang->linker,
6247                       (($seen_libtool ? '$(LIBTOOL) --mode=link ' : '')
6248                        . $lang->link));
6249 }
6250
6251 ################################################################
6252
6253 ## ---------------- ##
6254 ## Handling rules.  ##
6255 ## ---------------- ##
6256
6257 sub rule_define ($$$$)
6258 {
6259   my ($target, $rule_is_am, $cond, $where) = @_;
6260
6261   if (defined $targets{$target}
6262       && ($cond
6263           ? ! defined $target_conditional{$target}
6264           : defined $target_conditional{$target}))
6265     {
6266       &am_line_error ($target,
6267                       "$target defined both conditionally and unconditionally");
6268     }
6269
6270   # Value here doesn't matter; for targets we only note existence.
6271   $targets{$target} = $where;
6272   if ($cond)
6273     {
6274       if ($target_conditional{$target})
6275         {
6276           &check_ambiguous_conditional ($target, $cond);
6277         }
6278       $target_conditional{$target}{$cond} = $where;
6279     }
6280
6281
6282   # Check the rule for being a suffix rule. If so, store in a hash.
6283
6284   if ((my ($source_suffix, $object_suffix)) = ($target =~ $SUFFIX_RULE_PATTERN))
6285   {
6286     $suffix_rules{$source_suffix} = $object_suffix;
6287     print "Sources ending in .$source_suffix become .$object_suffix\n"
6288       if $verbose;
6289     # Set SUFFIXES from suffix_rules.
6290     push @suffixes, ".$source_suffix", ".$object_suffix";
6291   }
6292 }
6293
6294
6295 # See if a target exists.
6296 sub target_defined
6297 {
6298     my ($target) = @_;
6299     return defined $targets{$target};
6300 }
6301
6302
6303 ################################################################
6304
6305 # Read Makefile.am and set up %contents.  Simultaneously copy lines
6306 # from Makefile.am into $output_trailer or $output_vars as
6307 # appropriate.  NOTE we put rules in the trailer section.  We want
6308 # user rules to come after our generated stuff.
6309 sub read_am_file
6310 {
6311     my ($amfile) = @_;
6312
6313     my $am_file = new IO::File ("< $amfile");
6314     if (! $am_file)
6315     {
6316         die "$me: couldn't open `$amfile': $!\n";
6317     }
6318     print "$me: reading $amfile\n" if $verbose;
6319
6320     my $spacing = '';
6321     my $comment = '';
6322     my $blank = 0;
6323
6324     while ($_ = $am_file->getline)
6325     {
6326         if (/$IGNORE_PATTERN/o)
6327         {
6328             # Merely delete comments beginning with two hashes.
6329         }
6330         elsif (/$WHITE_PATTERN/o)
6331         {
6332             # Stick a single white line before the incoming macro or rule.
6333             $spacing = "\n";
6334             $blank = 1;
6335         }
6336         elsif (/$COMMENT_PATTERN/o)
6337         {
6338             # Stick comments before the incoming macro or rule.  Make
6339             # sure a blank line preceeds first block of comments.
6340             $spacing = "\n" unless $blank;
6341             $blank = 1;
6342             $comment .= $spacing . $_;
6343             $spacing = '';
6344         }
6345         else
6346         {
6347             last;
6348         }
6349     }
6350
6351     $output_vars .= $comment . "\n";
6352     $comment = '';
6353     $spacing = "\n";
6354
6355     # We save the conditional stack on entry, and then check to make
6356     # sure it is the same on exit.  This lets us conditonally include
6357     # other files.
6358     my @saved_cond_stack = @cond_stack;
6359     my $cond = conditional_string (@cond_stack);
6360
6361     my $saw_bk = 0;
6362     my $was_rule = 0;
6363     my $last_var_name = '';
6364     my $last_var_type = '';
6365     my $last_var_value = '';
6366     # FIXME: shouldn't use $_ in this loop; it is too big.
6367     while ($_)
6368     {
6369         $_ .= "\n"
6370             unless substr ($_, -1, 1) eq "\n";
6371
6372         # Don't look at MAINTAINER_MODE_TRUE here.  That shouldn't be
6373         # used by users.  @MAINT@ is an anachronism now.
6374         $_ =~ s/\@MAINT\@//g
6375             unless $seen_maint_mode;
6376
6377         my $new_saw_bk = /\\$/;
6378
6379         if (/$IGNORE_PATTERN/o)
6380         {
6381             # Merely delete comments beginning with two hashes.
6382         }
6383         elsif (/$WHITE_PATTERN/o)
6384         {
6385             # Stick a single white line before the incoming macro or rule.
6386             $spacing = "\n";
6387             &am_line_error ($., "blank line following trailing backslash")
6388                 if $saw_bk;
6389         }
6390         elsif (/$COMMENT_PATTERN/o)
6391         {
6392             # Stick comments before the incoming macro or rule.
6393             $comment .= $spacing . $_;
6394             $spacing = '';
6395             &am_line_error ($., "comment following trailing backslash")
6396                 if $saw_bk;
6397         }
6398         elsif ($saw_bk)
6399         {
6400             if ($was_rule)
6401             {
6402                 $output_trailer .= &make_condition (@cond_stack);
6403                 $output_trailer .= $_;
6404             }
6405             else
6406             {
6407               $last_var_value .= ' '
6408                 unless $last_var_value =~ /\s$/;
6409               $last_var_value .= $_;
6410
6411               if (!/\\$/)
6412                 {
6413                   $var_comment{$last_var_name} .= "$spacing"
6414                     if (!defined $var_comment{$last_var_name}
6415                         || substr ($var_comment{$last_var_name}, -1) ne "\n");
6416                   $var_comment{$last_var_name} .= "$comment";
6417                   $comment = $spacing = '';
6418                   macro_define ($last_var_name, 0,
6419                                 $last_var_type, $cond,
6420                                 $last_var_value, $.)
6421                     if $cond ne 'FALSE';
6422                   push (@var_list, $last_var_name);
6423                 }
6424             }
6425         }
6426
6427         elsif (/$IF_PATTERN/o)
6428           {
6429             $cond = cond_stack_if ($1, $2, "$amfile:$.");
6430           }
6431         elsif (/$ELSE_PATTERN/o)
6432           {
6433             $cond = cond_stack_else ($1, $2, "$amfile:$.");
6434           }
6435         elsif (/$ENDIF_PATTERN/o)
6436           {
6437             $cond = cond_stack_endif ($1, $2, "$amfile:$.");
6438           }
6439
6440         elsif (/$RULE_PATTERN/o)
6441         {
6442             # Found a rule.
6443             $was_rule = 1;
6444
6445             rule_define ($1, 0, $cond, $.);
6446
6447             $var_line{$1} = $.;
6448             $output_trailer .= $comment . $spacing;
6449             $output_trailer .= &make_condition (@cond_stack);
6450             $output_trailer .= $_;
6451             $comment = $spacing = '';
6452         }
6453         elsif (/$ASSIGNMENT_PATTERN/o)
6454         {
6455             # Found a macro definition.
6456             $was_rule = 0;
6457             $last_var_name = $1;
6458             $last_var_type = $2;
6459             $last_var_value = $3;
6460             if ($3 ne '' && substr ($3, -1) eq "\\")
6461             {
6462                 # We preserve the `\' because otherwise the long lines
6463                 # that are generated will be truncated by broken
6464                 # `sed's.
6465                 $last_var_value = $3 . "\n";
6466             }
6467
6468             if (!/\\$/)
6469               {
6470                 # FIXME: this doesn't always work correctly; it will
6471                 # group all comments for a given variable, no matter
6472                 # where defined.
6473                 # Accumulating variables must not be output.
6474                 $var_comment{$last_var_name} .= "$spacing"
6475                   if (!defined $var_comment{$last_var_name}
6476                       || substr ($var_comment{$last_var_name}, -1) ne "\n");
6477                 $var_comment{$last_var_name} .= "$comment";
6478                 $comment = $spacing = '';
6479
6480                 macro_define ($last_var_name, 0,
6481                               $last_var_type, $cond,
6482                               $last_var_value, $.)
6483                   if $cond ne 'FALSE';
6484                 push (@var_list, $last_var_name);
6485               }
6486         }
6487         elsif (/$INCLUDE_PATTERN/o)
6488         {
6489             my $path = $1;
6490
6491             if ($path =~ s/^\$\(top_srcdir\)\///)
6492             {
6493                 push (@include_stack, "\$\(top_srcdir\)/$path");
6494             }
6495             else
6496             {
6497                 $path =~ s/\$\(srcdir\)\///;
6498                 push (@include_stack, "\$\(srcdir\)/$path");
6499                 $path = $relative_dir . "/" . $path;
6500             }
6501             &read_am_file ($path);
6502         }
6503         else
6504         {
6505             # This isn't an error; it is probably a continued rule.
6506             # In fact, this is what we assume.
6507             $was_rule = 1;
6508             $output_trailer .= $comment . $spacing;
6509             $output_trailer .= &make_condition  (@cond_stack);
6510             $output_trailer .= $_;
6511             $comment = $spacing = '';
6512         }
6513
6514         $saw_bk = $new_saw_bk;
6515         $_ = $am_file->getline;
6516     }
6517
6518     $output_trailer .= $comment;
6519
6520     if (join (' ', @saved_cond_stack) ne join (' ', @cond_stack))
6521     {
6522         if (@cond_stack)
6523         {
6524             &am_error ("unterminated conditionals: @cond_stack");
6525         }
6526         else
6527         {
6528             # FIXME: better error message here.
6529             &am_error ("conditionals not nested in include file");
6530         }
6531     }
6532 }
6533
6534
6535 # define_standard_variables ()
6536 # ----------------------------
6537 # A helper for read_main_am_file which initializes configure variables
6538 # and variables from header-vars.am.  This is a subr so we can call it
6539 # twice.
6540 sub define_standard_variables
6541 {
6542     my $saved_output_vars = $output_vars;
6543     my ($comments, undef, $rules) =
6544       file_contents_internal (1, "$libdir/am/header-vars.am");
6545
6546     # This will output the definitions in $output_vars, which we don't
6547     # want...
6548     foreach my $var (sort keys %configure_vars)
6549     {
6550         &define_configure_variable ($var);
6551         push (@var_list, $var);
6552     }
6553
6554     # ... hence, we restore $output_vars.
6555     $output_vars = $saved_output_vars . $comments . $rules;
6556 }
6557
6558 # Read main am file.
6559 sub read_main_am_file
6560 {
6561     my ($amfile) = @_;
6562
6563     # This supports the strange variable tricks we are about to play.
6564     if (scalar keys %var_value > 0)
6565       {
6566         macros_dump ();
6567         &prog_error ("variable defined before read_main_am_file");
6568       }
6569
6570     # Generate copyright header for generated Makefile.in.
6571     # We do discard the output of predefined variables, handled below.
6572     $output_vars = ("# $in_file_name generated automatically by automake "
6573                    . $VERSION . " from $am_file_name.\n");
6574     $output_vars .= $gen_copyright;
6575
6576     # We want to predefine as many variables as possible.  This lets
6577     # the user set them with `+=' in Makefile.am.  However, we don't
6578     # want these initial definitions to end up in the output quite
6579     # yet.  So we just load them, but output them later.
6580     &define_standard_variables;
6581
6582     # Read user file, which might override some of our values.
6583     &read_am_file ($amfile);
6584
6585     # Ouput all the Automake variables.  If the user changed one, then
6586     # it is now marked as owned by the user.
6587     foreach my $var (uniq @var_list)
6588     {
6589         # Don't process user variables.
6590         variable_output ($var)
6591           unless !$var_is_am{$var};
6592     }
6593
6594     # Now dump the user variables that were defined.  We do it in the same
6595     # order in which they were defined (skipping duplicates).
6596     foreach my $var (uniq @var_list)
6597     {
6598         # Don't process Automake variables.
6599         variable_output ($var)
6600           unless $var_is_am{$var};
6601     }
6602 }
6603
6604 ################################################################
6605
6606 # $FLATTENED
6607 # &flatten ($STRING)
6608 # ------------------
6609 # Flatten the $STRING and return the result.
6610 sub flatten
6611 {
6612   $_ = shift;
6613
6614   s/\\\n//somg;
6615   s/\s+/ /g;
6616   s/^ //;
6617   s/ $//;
6618
6619   return $_;
6620 }
6621
6622
6623 # @PARAGRAPHS
6624 # &make_paragraphs ($MAKEFILE, [%TRANSFORM])
6625 # ------------------------------------------
6626 # Load a $MAKEFILE, apply the %TRANSFORM, and return it as a list of
6627 # paragraphs.
6628 sub make_paragraphs ($%)
6629 {
6630     my ($file, %transform) = @_;
6631
6632     # Complete %transform with global options and make it a Perl
6633     # $command.
6634     my $command =
6635       "s/$IGNORE_PATTERN//gm;"
6636         . transform (%transform,
6637
6638                      'CYGNUS'          => $cygnus_mode,
6639                      'MAINTAINER-MODE'
6640                      => $seen_maint_mode ? subst ('MAINTAINER_MODE_TRUE') : '',
6641
6642                      'SHAR'        => $options{'dist-shar'} || 0,
6643                      'BZIP2'       => $options{'dist-bzip2'} || 0,
6644                      'ZIP'         => $options{'dist-zip'} || 0,
6645                      'COMPRESS'    => $options{'dist-tarZ'} || 0,
6646
6647                      'INSTALL-INFO' => !$options{'no-installinfo'},
6648                      'INSTALL-MAN'  => !$options{'no-installman'},
6649                      'CK-NEWS'      => $options{'check-news'} || 0,
6650
6651                      'SUBDIRS'      => &variable_defined ('SUBDIRS'),
6652                      'TOPDIR'       => backname ($relative_dir),
6653                      'TOPDIR_P'     => $relative_dir eq '.',
6654                      'CONFIGURE-AC' => $configure_ac,
6655
6656                      'BUILD'    => $seen_canonical == $AC_CANONICAL_SYSTEM,
6657                      'HOST'     => $seen_canonical,
6658                      'TARGET'   => $seen_canonical == $AC_CANONICAL_SYSTEM,
6659
6660                      'LIBTOOL'      => defined $configure_vars{'LIBTOOL'})
6661           # We don't need more than two consecutive new-lines.
6662           . 's/\n{3,}/\n\n/g';
6663
6664     # Swallow the file and apply the COMMAND.
6665     my $fc_file = new IO::File ("< $file");
6666     if (! $fc_file)
6667     {
6668         die "$me: installation error: cannot open `$file'\n";
6669     }
6670     # Looks stupid?
6671     print "$me: reading $file\n"
6672       if $verbose;
6673     my $saved_dollar_slash = $/;
6674     undef $/;
6675     $_ = $fc_file->getline;
6676     $/ = $saved_dollar_slash;
6677     eval $command;
6678     $fc_file->close;
6679     my $content = $_;
6680
6681     # Split at unescaped new lines.
6682     my @lines = split (/(?<!\\)\n/, $content);
6683     my @res;
6684
6685     while (defined ($_ = shift @lines))
6686       {
6687         my $paragraph = "$_";
6688         # If we are a rule, eat as long as we start with a tab.
6689         if (/$RULE_PATTERN/smo)
6690           {
6691             while (defined ($_ = shift @lines) && $_ =~ /^\t/)
6692               {
6693                 $paragraph .= "\n$_";
6694               }
6695             unshift (@lines, $_);
6696           }
6697
6698         # If we are a comments, eat as much comments as you can.
6699         elsif (/$COMMENT_PATTERN/smo)
6700           {
6701             while (defined ($_ = shift @lines)
6702                    && $_ =~ /$COMMENT_PATTERN/smo)
6703               {
6704                 $paragraph .= "\n$_";
6705               }
6706             unshift (@lines, $_);
6707           }
6708
6709         push @res, $paragraph;
6710         $paragraph = '';
6711       }
6712
6713     return @res;
6714 }
6715
6716
6717
6718 # ($COMMENT, $VARIABLES, $RULES)
6719 # &file_contents_internal ($IS_AM, $FILE, [%TRANSFORM])
6720 # -----------------------------------------------------
6721 # Return contents of a file from $libdir/am, automatically skipping
6722 # macros or rules which are already known. $IS_AM iff the caller is
6723 # reading an Automake file (as opposed to the user's Makefile.am).
6724 sub file_contents_internal ($$%)
6725 {
6726     my ($is_am, $file, %transform) = @_;
6727
6728     my $result_vars = '';
6729     my $result_rules = '';
6730     my $comment = '';
6731     my $spacing = '';
6732
6733     # We save the conditional stack on entry, and then check to make
6734     # sure it is the same on exit.  This lets us conditonally include
6735     # other files.
6736     my @saved_cond_stack = @cond_stack;
6737     my $cond = conditional_string (@cond_stack);
6738
6739     foreach (make_paragraphs ($file, %transform))
6740     {
6741         # Sanity checks.
6742         &am_file_error ($file, "blank line following trailing backslash:\n$_")
6743           if /\\$/;
6744         &am_file_error ($file, "comment following trailing backslash:\n$_")
6745           if /\\#/;
6746
6747         if (/^$/)
6748         {
6749             # Stick empty line before the incoming macro or rule.
6750             $spacing = "\n";
6751         }
6752         elsif (/$COMMENT_PATTERN/mso)
6753         {
6754             # Stick comments before the incoming macro or rule.
6755             $comment = "$_\n";
6756         }
6757
6758         # Handle inclusion of other files.
6759         elsif (/$INCLUDE_PATTERN/o)
6760         {
6761             if ($cond ne 'FALSE')
6762               {
6763                 my $file = ($is_am ? "$libdir/am/" : '') . $1;
6764                 # N-ary `.=' fails.
6765                 my ($com, $vars, $rules)
6766                   = file_contents_internal ($is_am, $file, %transform);
6767                 $comment .= $com;
6768                 $result_vars .= $vars;
6769                 $result_rules .= $rules;
6770               }
6771         }
6772
6773         # Handling the conditionals.
6774         elsif (/$IF_PATTERN/o)
6775           {
6776             $cond = cond_stack_if ($1, $2, $file);
6777           }
6778         elsif (/$ELSE_PATTERN/o)
6779           {
6780             $cond = cond_stack_else ($1, $2, $file);
6781           }
6782         elsif (/$ENDIF_PATTERN/o)
6783           {
6784             $cond = cond_stack_endif ($1, $2, $file);
6785           }
6786
6787         # Handling rules.
6788         elsif (/$RULE_PATTERN/mso)
6789         {
6790           # Separate relationship from optional actions: the first
6791           # `new-line tab" not preceded by backslash (continuation
6792           # line).
6793           # I'm quite shoked!  It seems that (\\\n|[^\n]) is not the
6794           # same as `([^\n]|\\\n)!!!  Don't swap it, it breaks.
6795           my $paragraph = $_;
6796           /^((?:\\\n|[^\n])*)(?:\n(\t.*))?$/som;
6797           my ($relationship, $actions) = ($1, $2 || '');
6798
6799           # Separate targets from dependencies: the first colon.
6800           $relationship =~ /^([^:]+\S+) *: *(.*)$/som;
6801           my ($targets, $dependencies) = ($1, $2);
6802           # Remove the escaped new lines.
6803           # I don't know why, but I have to use a tmp $flat_deps.
6804           my $flat_deps = &flatten ($dependencies);
6805           my @deps = split (' ', $flat_deps);
6806
6807           foreach (split (' ' , $targets))
6808             {
6809               # FIXME: We are not robust to people defining several targets
6810               # at once, only some of them being in %dependencies.
6811
6812               # Output only if not in FALSE.
6813               if (defined $dependencies{$_}
6814                   && $cond ne 'FALSE')
6815                 {
6816                   &depend ($_, @deps);
6817                   $actions{$_} .= $actions;
6818                 }
6819               else
6820                 {
6821                   # Free lance dependency.  Output the rule for all the
6822                   # targets instead of one by one.
6823                   if (!defined $targets{$targets}
6824                       && $cond ne 'FALSE')
6825                     {
6826                       $paragraph =~ s/^/make_condition (@cond_stack)/gme;
6827                       $result_rules .= "$spacing$comment$paragraph\n";
6828                       rule_define ($targets, $is_am, $cond, $file);
6829                     }
6830                   $comment = $spacing = '';
6831                   last;
6832                 }
6833             }
6834         }
6835
6836         elsif (/$ASSIGNMENT_PATTERN/mso)
6837         {
6838             my ($var, $type, $val) = ($1, $2, $3);
6839             &am_file_error ($file, "macro `$var' with trailing backslash")
6840               if /\\$/;
6841
6842             # Accumulating variables must not be output.
6843             $var_comment{$var} .= "$spacing"
6844               if (!defined $var_comment{$var}
6845                   || substr ($var_comment{$var}, -1) ne "\n");
6846             $var_comment{$var} .= "$comment";
6847             macro_define ($var, $is_am, $type, $cond, $val, $file)
6848               if $cond ne 'FALSE';
6849             push (@var_list, $var);
6850
6851             # If the user has set some variables we were in charge
6852             # of (which is detected by the first reading of
6853             # `header-vars.am'), we must not output them.
6854             $result_vars .= "$spacing$comment$_\n"
6855               if $type ne '+' && $var_is_am{$var} && $cond ne 'FALSE';
6856
6857             $comment = $spacing = '';
6858         }
6859         else
6860         {
6861             # This isn't an error; it is probably some tokens which
6862             # configure is supposed to replace, such as `@SET-MAKE@',
6863             # or some part of a rule cut by an if/endif.
6864             if ($cond ne 'FALSE')
6865               {
6866                 s/^/make_condition (@cond_stack)/gme;
6867                 $result_rules .= "$spacing$comment$_\n";
6868               }
6869             $comment = $spacing = '';
6870         }
6871     }
6872
6873     if (join (' ', @saved_cond_stack) ne join (' ', @cond_stack))
6874     {
6875         if (@cond_stack)
6876         {
6877             &am_error ("unterminated conditionals: @cond_stack");
6878         }
6879         else
6880         {
6881             # FIXME: better error message here.
6882             &am_error ("conditionals not nested in include file");
6883         }
6884     }
6885
6886     return ($comment, $result_vars, $result_rules);
6887 }
6888
6889
6890 # $CONTENTS
6891 # &file_contents ($BASENAME, [%TRANSFORM])
6892 # ----------------------------------------
6893 # Return contents of a file from $libdir/am, automatically skipping
6894 # macros or rules which are already known.
6895 sub file_contents ($%)
6896 {
6897     my ($basename, %transform) = @_;
6898     my ($comments, $variables, $rules) =
6899       file_contents_internal (1, "$libdir/am/$basename.am", %transform);
6900     return "$comments$variables$rules";
6901 }
6902
6903
6904 # $REGEXP
6905 # &transform (%PAIRS)
6906 # -------------------
6907 # Foreach ($TOKEN, $VAL) in %PAIRS produce a replacement expression suitable
6908 # for file_contents which:
6909 #   - replaces @$TOKEN@ with $VALUE,
6910 #   - enables/disables ?$TOKEN?.
6911 sub transform (%)
6912 {
6913     my (%pairs) = @_;
6914     my $result = '';
6915
6916     while (my ($token, $val) = each %pairs)
6917     {
6918         $result .= "s/\Q%$token%\E/\Q$val\E/gm;";
6919         if ($val)
6920         {
6921             $result .= "s/\Q?$token?\E//gm;s/^.*\Q?!$token?\E.*\\n//gm;";
6922             $result .= "s/\Q%?$token%\E/TRUE/gm;";
6923         }
6924         else
6925         {
6926             $result .= "s/\Q?!$token?\E//gm;s/^.*\Q?$token?\E.*\\n//gm;";
6927             $result .= "s/\Q%?$token%\E/FALSE/gm;";
6928         }
6929     }
6930
6931     return $result;
6932 }
6933
6934
6935 # Find all variable prefixes that are used for install directories.  A
6936 # prefix `zar' qualifies iff:
6937 # * `zardir' is a variable.
6938 # * `zar_PRIMARY' is a variable.
6939 sub am_primary_prefixes
6940 {
6941     my ($primary, $can_dist, @prefixes) = @_;
6942
6943     my %valid = map { $_ => 0 } @prefixes;
6944     $valid{'EXTRA'} = 0;
6945     foreach my $varname (keys %var_value)
6946     {
6947         # Automake is allowed to define variables that look like they
6948         # are magic variables, such as INSTALL_DATA.
6949         next
6950           if $var_is_am{$varname};
6951
6952         if ($varname =~ /^(nobase_)?(dist_|nodist_)?(.*)_$primary$/)
6953         {
6954             my ($base, $dist, $X) = ($1 || '', $2 || '', $3 || '');
6955             if ($dist ne '' && ! $can_dist)
6956             {
6957                 # Note that a configure variable is always legitimate.
6958                 # It is natural to name such variables after the
6959                 # primary, so we explicitly allow it.
6960                 if (! defined $configure_vars{$varname})
6961                 {
6962                     &am_line_error ($varname,
6963                                     "invalid variable `$varname': `dist' is forbidden");
6964                 }
6965             }
6966             elsif (! defined $valid{$X} && ! &variable_defined ("${X}dir"))
6967             {
6968                 # Note that a configure variable is always legitimate.
6969                 # It is natural to name such variables after the
6970                 # primary, so we explicitly allow it.
6971                 if (! defined $configure_vars{$varname})
6972                 {
6973                     &am_line_error ($varname,
6974                                     "invalid variable `$varname'");
6975                 }
6976             }
6977             else
6978             {
6979                 # Ensure all extended prefixes are actually used.
6980                 $valid{"$base$dist$X"} = 1;
6981             }
6982         }
6983     }
6984
6985     return %valid;
6986 }
6987
6988 # Handle `where_HOW' variable magic.  Does all lookups, generates
6989 # install code, and possibly generates code to define the primary
6990 # variable.  The first argument is the name of the .am file to munge,
6991 # the second argument is the primary variable (eg HEADERS), and all
6992 # subsequent arguments are possible installation locations.  Returns
6993 # list of all values of all _HOW targets.
6994 #
6995 # FIXME: this should be rewritten to be cleaner.  It should be broken
6996 # up into multiple functions.
6997 #
6998 # Usage is: am_install_var (OPTION..., file, HOW, where...)
6999 sub am_install_var
7000 {
7001     my (@args) = @_;
7002
7003     my $do_require = 1;
7004     my $can_dist = 0;
7005     my $default_dist = 0;
7006     while (@args)
7007     {
7008         if ($args[0] eq '-noextra')
7009         {
7010             $do_require = 0;
7011         }
7012         elsif ($args[0] eq '-candist')
7013         {
7014             $can_dist = 1;
7015         }
7016         elsif ($args[0] eq '-defaultdist')
7017         {
7018             $default_dist = 1;
7019             $can_dist = 1;
7020         }
7021         elsif ($args[0] !~ /^-/)
7022         {
7023             last;
7024         }
7025         shift (@args);
7026     }
7027
7028     my ($file, $primary, @prefixes) = @args;
7029
7030     # Now that configure substitutions are allowed in where_HOW
7031     # variables, it is an error to actually define the primary.  We
7032     # allow `JAVA', as it is customarily used to mean the Java
7033     # interpreter.  This is but one of several Java hacks.  Similarly,
7034     # `PYTHON' is customarily used to mean the Python interpreter.
7035     &am_line_error ($primary, "`$primary' is an anachronism")
7036         if &variable_defined ($primary)
7037             && ($primary ne 'JAVA' && $primary ne 'PYTHON');
7038
7039
7040     # Look for misspellings.  It is an error to have a variable ending
7041     # in a "reserved" suffix whose prefix is unknown, eg
7042     # "bni_PROGRAMS".  However, unusual prefixes are allowed if a
7043     # variable of the same name (with "dir" appended) exists.  For
7044     # instance, if the variable "zardir" is defined, then
7045     # "zar_PROGRAMS" becomes valid.  This is to provide a little extra
7046     # flexibility in those cases which need it.
7047     my %valid = &am_primary_prefixes ($primary, $can_dist, @prefixes);
7048
7049     # If a primary includes a configure substitution, then the EXTRA_
7050     # form is required.  Otherwise we can't properly do our job.
7051     my $require_extra;
7052     my $warned_about_extra = 0;
7053
7054     my @used = ();
7055     my @result = ();
7056
7057     # True if the iteration is the first one.  Used for instance to
7058     # output parts of the associated file only once.
7059     my $first = 0;
7060     foreach my $X (sort keys %valid)
7061     {
7062         my $one_name = $X . '_' . $primary;
7063         next
7064           unless (&variable_defined ($one_name));
7065
7066         my $strip_subdir = 1;
7067         # If subdir prefix should be preserved, do so.
7068         if ($X =~ /^nobase_/)
7069           {
7070             $strip_subdir = 0;
7071             $X =~ s/^nobase_//;
7072           }
7073
7074         my $nodir_name = $X;
7075         # If files should be distributed, do so.
7076         my $dist_p = 0;
7077         if ($can_dist)
7078           {
7079             $dist_p = (($default_dist && $one_name !~ /^nodist_/)
7080                        || (! $default_dist && $one_name =~ /^dist_/));
7081             $nodir_name =~ s/^(dist|nodist)_//;
7082           }
7083
7084         # Append actual contents of where_PRIMARY variable to
7085         # result.
7086         foreach my $rcurs (&variable_value_as_list ($one_name, 'all'))
7087           {
7088             # Skip configure substitutions.  Possibly bogus.
7089             if ($rcurs =~ /^\@.*\@$/)
7090               {
7091                 if ($X eq 'EXTRA')
7092                   {
7093                     if (! $warned_about_extra)
7094                       {
7095                         $warned_about_extra = 1;
7096                         &am_line_error ($one_name,
7097                                         "`$one_name' contains configure substitution, but shouldn't");
7098                       }
7099                   }
7100                 # Check here to make sure variables defined in
7101                 # configure.ac do not imply that EXTRA_PRIMARY
7102                 # must be defined.
7103                 elsif (! defined $configure_vars{$one_name})
7104                   {
7105                     $require_extra = $one_name
7106                       if $do_require;
7107                   }
7108
7109                 next;
7110               }
7111
7112             push (@result, $rcurs);
7113           }
7114
7115         # A blatant hack: we rewrite each _PROGRAMS primary to
7116         # include EXEEXT when in Cygwin32 mode.
7117         if ($primary eq 'PROGRAMS')
7118           {
7119             my @conds = &variable_conditions ($one_name);
7120
7121             my @condvals;
7122             foreach my $cond (@conds)
7123               {
7124                 my @one_binlist = ();
7125                 my @condval = &variable_value_as_list ($one_name,
7126                                                        $cond);
7127                 foreach my $rcurs (@condval)
7128                   {
7129                     if ($rcurs =~ /\./ || $rcurs =~ /^\@.*\@$/)
7130                       {
7131                         push (@one_binlist, $rcurs);
7132                       }
7133                     else
7134                       {
7135                         push (@one_binlist, $rcurs . '$(EXEEXT)');
7136                       }
7137                   }
7138
7139                 push (@condvals, $cond);
7140                 push (@condvals, join (' ', @one_binlist));
7141               }
7142
7143             variable_delete ($one_name);
7144             while (@condvals)
7145               {
7146                 my $cond = shift (@condvals);
7147                 my @val = split (' ', shift (@condvals));
7148                 &define_pretty_variable ($one_name, $cond, @val);
7149               }
7150           }
7151
7152         # "EXTRA" shouldn't be used when generating clean targets,
7153         # all, or install targets.
7154         if ($X eq 'EXTRA')
7155           {
7156             # We used to warn if EXTRA_FOO was defined uselessly,
7157             # but this was annoying.
7158             next;
7159           }
7160
7161         if ($X eq 'check')
7162           {
7163             push (@check, '$(' . $one_name . ')');
7164           }
7165         else
7166           {
7167             push (@used, '$(' . $one_name . ')');
7168           }
7169
7170         # Is this to be installed?
7171         my $install_p = $X ne 'noinst' && $X ne 'check';
7172
7173         # If so, with install-exec? (or install-data?).
7174         my $exec_p = (defined $exec_dir_p {$X}
7175                       ? $exec_dir_p {$X}
7176                       : ($X =~ /exec/));
7177
7178         # Singular form of $PRIMARY.
7179         (my $one_primary = $primary) =~ s/S$//;
7180         $output_rules .= &file_contents ($file,
7181                                          ('FIRST' => $first,
7182
7183                                           'PRIMARY'     => $primary,
7184                                           'ONE_PRIMARY' => $one_primary,
7185                                           'DIR'         => $X,
7186                                           'NDIR'        => $nodir_name,
7187                                           'BASE'        => $strip_subdir,
7188
7189                                           'EXEC'    => $exec_p,
7190                                           'INSTALL' => $install_p,
7191                                           'DIST'    => $dist_p));
7192
7193         $first = '';
7194     }
7195
7196     # The JAVA variable is used as the name of the Java interpreter.
7197     # The PYTHON variable is used as the name of the Python interpreter.
7198     if (@used && $primary ne 'JAVA' && $primary ne 'PYTHON')
7199     {
7200         # Define it.
7201         &define_pretty_variable ($primary, '', @used);
7202         $output_vars .= "\n";
7203     }
7204
7205     if ($require_extra && ! &variable_defined ('EXTRA_' . $primary))
7206     {
7207         &am_line_error ($require_extra,
7208                         "`$require_extra' contains configure substitution, but `EXTRA_$primary' not defined");
7209     }
7210
7211     # Push here because PRIMARY might be configure time determined.
7212     push (@all, '$(' . $primary . ')')
7213         if @used && $primary ne 'JAVA' && $primary ne 'PYTHON';
7214
7215     # Make the result unique.  This lets the user use conditionals in
7216     # a natural way, but still lets us program lazily -- we don't have
7217     # to worry about handling a particular object more than once.
7218     return uniq (sort @result);
7219 }
7220
7221
7222 ################################################################
7223
7224 # Each key in this hash is the name of a directory holding a
7225 # Makefile.in.  These variables are local to `is_make_dir'.
7226 my %make_dirs = ();
7227 my $make_dirs_set = 0;
7228
7229 sub is_make_dir
7230 {
7231     my ($dir) = @_;
7232     if (! $make_dirs_set)
7233     {
7234         foreach my $iter (@configure_input_files)
7235         {
7236             $make_dirs{dirname ($iter)} = 1;
7237         }
7238         # We also want to notice Makefile.in's.
7239         foreach my $iter (@other_input_files)
7240         {
7241             if ($iter =~ /Makefile\.in$/)
7242             {
7243                 $make_dirs{dirname ($iter)} = 1;
7244             }
7245         }
7246         $make_dirs_set = 1;
7247     }
7248     return defined $make_dirs{$dir};
7249 }
7250
7251 ################################################################
7252
7253 # This variable is local to the "require file" set of functions.
7254 my @require_file_paths = ();
7255
7256 # If a file name appears as a key in this hash, then it has already
7257 # been checked for.  This variable is local to the "require file"
7258 # functions.
7259 %require_file_found = ();
7260
7261 # See if we want to push this file onto dist_common.  This function
7262 # encodes the rules for deciding when to do so.
7263 sub maybe_push_required_file
7264 {
7265     my ($dir, $file, $fullfile) = @_;
7266
7267     if ($dir eq $relative_dir)
7268     {
7269         &push_dist_common ($file);
7270     }
7271     elsif ($relative_dir eq '.' && ! &is_make_dir ($dir))
7272     {
7273         # If we are doing the topmost directory, and the file is in a
7274         # subdir which does not have a Makefile, then we distribute it
7275         # here.
7276         &push_dist_common ($fullfile);
7277     }
7278 }
7279
7280
7281 # &require_file_internal ($IS_CONFIGURE, $LINE, $MYSTRICT, @FILES)
7282 # ----------------------------------------------------------------
7283 # Verify that the file must exist in the current directory.
7284 # $MYSTRICT is the strictness level at which this file becomes required.
7285 #
7286 # Must set require_file_paths before calling this function.
7287 # require_file_paths is set to hold a single directory (the one in
7288 # which the first file was found) before return.
7289 sub require_file_internal
7290 {
7291     my ($is_configure, $line, $mystrict, @files) = @_;
7292
7293     foreach my $file (@files)
7294     {
7295         my $fullfile;
7296         my $errdir;
7297         my $errfile;
7298         my $save_dir;
7299
7300         # If we've already looked for it, we're done.
7301         next if defined $require_file_found{$file};
7302         $require_file_found{$file} = 1;
7303
7304         my $found_it = 0;
7305         my $dangling_sym = 0;
7306         foreach my $dir (@require_file_paths)
7307         {
7308             $fullfile = $dir . "/" . $file;
7309             $errdir = $dir unless $errdir;
7310
7311             # Use different name for "error filename".  Otherwise on
7312             # an error the bad file will be reported as eg
7313             # `../../install-sh' when using the default
7314             # config_aux_path.
7315             $errfile = $errdir . '/' . $file;
7316
7317             if (-l $fullfile && ! -f readlink ($fullfile))
7318             {
7319                 $dangling_sym = 1;
7320                 last;
7321             }
7322             elsif (-f $fullfile)
7323             {
7324                 $found_it = 1;
7325                 &maybe_push_required_file ($dir, $file, $fullfile);
7326                 $save_dir = $dir;
7327                 last;
7328             }
7329         }
7330
7331         if ($found_it && ! $force_missing)
7332         {
7333             # Prune the path list.
7334             @require_file_paths = $save_dir;
7335         }
7336         else
7337         {
7338             if ($strictness >= $mystrict)
7339             {
7340                 if ($dangling_sym && $add_missing)
7341                 {
7342                     unlink ($fullfile);
7343                 }
7344
7345                 my $trailer = '';
7346                 my $suppress = 0;
7347
7348                 # Only install missing files according to our desired
7349                 # strictness level.
7350                 my $message = "required file `$errfile' not found";
7351                 if ($add_missing)
7352                 {
7353                     $suppress = 1;
7354
7355                     # Maybe run libtoolize.
7356                     my @syslist = ('libtoolize', '--automake');
7357                     push @syslist, '--copy'
7358                         if $copy_missing;
7359                     if ($seen_libtool
7360                         && grep ($_ eq $file, @libtoolize_files)
7361                         && system (@syslist))
7362                     {
7363                         $message = "installing `$errfile'";
7364                         $suppress = 0;
7365                         $trailer = "; cannot run `libtoolize': $!";
7366                     }
7367                     elsif (-f ("$libdir/$file"))
7368                     {
7369                         # Install the missing file.  Symlink if we
7370                         # can, copy if we must.  Note: delete the file
7371                         # first, in case it is a dangling symlink.
7372                         $message = "installing `$errfile'";
7373                         # Windows Perl will hang if we try to delete a
7374                         # file that doesn't exist.
7375                         unlink ($errfile) if -f $errfile;
7376                         if ($symlink_exists && ! $copy_missing)
7377                         {
7378                             if (! symlink ("$libdir/$file", $errfile))
7379                             {
7380                                 $suppress = 0;
7381                                 $trailer = "; error while making link: $!";
7382                             }
7383                         }
7384                         elsif (system ('cp', "$libdir/$file", $errfile))
7385                         {
7386                             $suppress = 0;
7387                             $trailer = "\n    error while copying";
7388                         }
7389                     }
7390
7391                     &maybe_push_required_file (dirname ($errfile),
7392                                                $file, $errfile);
7393
7394                     # Prune the path list.
7395                     @require_file_paths = &dirname ($errfile);
7396                 }
7397
7398                 if ($suppress)
7399                 {
7400                     if ($is_configure)
7401                     {
7402                         # FIXME: allow actual file to be specified.
7403                         &am_conf_line_warning ($configure_ac, $line,
7404                                                "$message$trailer");
7405                     }
7406                     else
7407                     {
7408                         &am_line_warning ($line, "$message$trailer");
7409                     }
7410                 }
7411                 else
7412                 {
7413                     if ($is_configure)
7414                     {
7415                         # FIXME: allow actual file to be specified.
7416                         &am_conf_line_error ($configure_ac, $line,
7417                                              "$message$trailer");
7418                     }
7419                     else
7420                     {
7421                         &am_line_error ($line, "$message$trailer");
7422                     }
7423                 }
7424             }
7425         }
7426     }
7427 }
7428
7429 # Like require_file_with_line, but error messages refer to
7430 # configure.ac, not the current Makefile.am.
7431 sub require_file_with_conf_line
7432 {
7433     @require_file_paths = $relative_dir;
7434     &require_file_internal (1, @_);
7435 }
7436
7437 sub require_file_with_line
7438 {
7439     @require_file_paths = $relative_dir;
7440     &require_file_internal (0, @_);
7441 }
7442
7443 sub require_file
7444 {
7445     @require_file_paths = $relative_dir;
7446     &require_file_internal (0, '', @_);
7447 }
7448
7449 # Require a file that is also required by Autoconf.  Looks in
7450 # configuration path, as specified by AC_CONFIG_AUX_DIR.
7451 sub require_config_file
7452 {
7453     @require_file_paths = @config_aux_path;
7454     &require_file_internal (1, '', @_);
7455     my $dir = $require_file_paths[0];
7456     @config_aux_path = @require_file_paths;
7457      # Avoid unsightly '/.'s.
7458     $config_aux_dir = '$(top_srcdir)' . ($dir eq '.' ? "" : "/$dir");
7459 }
7460
7461 # Assumes that the line number is in Makefile.am.
7462 sub require_conf_file_with_line
7463 {
7464     @require_file_paths = @config_aux_path;
7465     &require_file_internal (0, @_);
7466     my $dir = $require_file_paths[0];
7467     @config_aux_path = @require_file_paths;
7468      # Avoid unsightly '/.'s.
7469     $config_aux_dir = '$(top_srcdir)' . ($dir eq '.' ? "" : "/$dir");
7470 }
7471
7472 # Assumes that the line number is in configure.ac.
7473 sub require_conf_file_with_conf_line
7474 {
7475     @require_file_paths = @config_aux_path;
7476     &require_file_internal (1, @_);
7477     my $dir = $require_file_paths[0];
7478     @config_aux_path = @require_file_paths;
7479     # avoid unsightly '/.'s.
7480     $config_aux_dir = '$(top_srcdir)' . ($dir eq '.' ? "" : "/$dir");
7481 }
7482
7483 ################################################################
7484
7485 # Push a list of files onto dist_common.
7486 sub push_dist_common
7487 {
7488     &prog_error ("push_dist_common run after handle_dist")
7489         if $handle_dist_run;
7490     macro_define ('DIST_COMMON', 1, '+', '', join (' ', @_), '');
7491 }
7492
7493
7494 # Set strictness.
7495 sub set_strictness
7496 {
7497     $strictness_name = $_[0];
7498     if ($strictness_name eq 'gnu')
7499     {
7500         $strictness = $GNU;
7501     }
7502     elsif ($strictness_name eq 'gnits')
7503     {
7504         $strictness = $GNITS;
7505     }
7506     elsif ($strictness_name eq 'foreign')
7507     {
7508         $strictness = $FOREIGN;
7509     }
7510     else
7511     {
7512         die "$me: level `$strictness_name' not recognized\n";
7513     }
7514 }
7515
7516
7517 ################################################################
7518
7519 # Ensure a file exists.
7520 sub create
7521 {
7522     my ($file) = @_;
7523
7524     my $touch = new IO::File (">> $file");
7525     $touch->close;
7526 }
7527
7528 # Glob something.  Do this to avoid indentation screwups everywhere we
7529 # want to glob.  Gross!
7530 sub my_glob
7531 {
7532     my ($pat) = @_;
7533     return <${pat}>;
7534 }
7535
7536 # Remove one level of brackets and strip leading spaces,
7537 # as does m4 to function arguments.
7538 sub unquote_m4_arg
7539 {
7540     $_ = shift;
7541     s/^\s*//;
7542
7543     my @letters = split //;
7544     my @result = ();
7545     my $depth = 0;
7546
7547     foreach (@letters)
7548     {
7549         if ($_ eq '[')
7550         {
7551             ++$depth;
7552             next if $depth == 1;
7553         }
7554         elsif ($_ eq ']')
7555         {
7556             --$depth;
7557             next if $depth == 0;
7558             # don't count orphan right brackets
7559             $depth = 0 if $depth < 0;
7560         }
7561         push @result, $_;
7562     }
7563     return join '', @result;
7564 }
7565
7566 ################################################################
7567
7568 # Print an error message and set exit status.
7569 sub am_error
7570 {
7571     warn "$me: ${am_file}.am: @_\n";
7572     $exit_status = 1;
7573 }
7574
7575 # am_file_error ($FILE, @ARGS)
7576 # ----------------------------
7577 sub am_file_error
7578 {
7579     my ($file, @args) = @_;
7580
7581     warn "$file: @args\n";
7582     $exit_status = 1;
7583 }
7584
7585 sub am_line_error
7586 {
7587     my ($symbol, @args) = @_;
7588
7589     if ($symbol && "$symbol" ne '-1')
7590     {
7591         my $file = "${am_file}.am";
7592
7593         if ($symbol =~ /^\d+$/)
7594         {
7595             # SYMBOL is a line number, so just add the colon.
7596             $file .= ':' . $symbol;
7597         }
7598         elsif (defined $var_line{$symbol})
7599         {
7600             # SYMBOL is a variable defined in Makefile.am, so add the
7601             # line number we saved from there.
7602             $file .= ':' . $var_line{$symbol};
7603         }
7604         elsif (defined $configure_vars{$symbol})
7605         {
7606             # SYMBOL is a variable defined in configure.ac, so add the
7607             # appropriate line number.
7608             $file = $configure_vars{$symbol};
7609         }
7610         else
7611         {
7612             # Couldn't find the line number.
7613         }
7614         warn $file, ": @args\n";
7615         $exit_status = 1;
7616     }
7617     else
7618     {
7619         &am_error (@args);
7620     }
7621 }
7622
7623 # Like am_error, but while scanning configure.ac.
7624 sub am_conf_error
7625 {
7626     # FIXME: can run in subdirs.
7627     warn "$me: $configure_ac: @_\n";
7628     $exit_status = 1;
7629 }
7630
7631 # Error message with line number referring to configure.ac.
7632 sub am_conf_line_error
7633 {
7634     my ($file, $line, @args) = @_;
7635
7636     if ($line)
7637     {
7638         warn "$file: $line: @args\n";
7639         $exit_status = 1;
7640     }
7641     else
7642     {
7643         &am_conf_error (@args);
7644     }
7645 }
7646
7647 # Warning message with line number referring to configure.ac.
7648 # Does not affect exit_status
7649 sub am_conf_line_warning
7650 {
7651     my $saved_exit_status = $exit_status;
7652     my $sig = $SIG{'__WARN__'};
7653     $SIG{'__WARN__'} = 'DEFAULT';
7654     am_conf_line_error (@_);
7655     $exit_status = $saved_exit_status;
7656     $SIG{'__WARN__'} = $sig;
7657 }
7658
7659 # Like am_line_error, but doesn't affect exit status.
7660 sub am_line_warning
7661 {
7662     my $saved_exit_status = $exit_status;
7663     my $sig = $SIG{'__WARN__'};
7664     $SIG{'__WARN__'} = 'DEFAULT';
7665     am_line_error (@_);
7666     $exit_status = $saved_exit_status;
7667     $SIG{'__WARN__'} = $sig;
7668 }
7669
7670 # Tell user where our aclocal.m4 is, but only once.
7671 sub keyed_aclocal_warning
7672 {
7673     my ($key) = @_;
7674     warn "$me: macro `$key' can be generated by `aclocal'\n";
7675 }
7676
7677 # Print usage information.
7678 sub usage
7679 {
7680     print <<EOF;
7681 Usage: $0 [OPTION] ... [Makefile]...
7682
7683 Generate Makefile.in for configure from Makefile.am.
7684
7685 Operation modes:
7686       --help             print this help, then exit
7687       --version          print version number, then exit
7688   -v, --verbose          verbosely list files processed
7689   -o, --output-dir=DIR   put generated Makefile.in's into DIR
7690       --no-force         only update Makefile.in's that are out of date
7691
7692 Dependency tracking:
7693   -i, --ignore-deps      disable dependency tracking code
7694       --include-deps     enable dependency tracking code
7695
7696 Flavors:
7697       --cygnus           assume program is part of Cygnus-style tree
7698       --foreign          set strictness to foreign
7699       --gnits            set strictness to gnits
7700       --gnu              set strictness to gnu
7701
7702 Library files:
7703   -a, --add-missing      add missing standard files to package
7704       --libdir=DIR       directory storing library files
7705   -c, --copy             with -a, copy missing files (default is symlink)
7706   -f, --force-missing    force update of standard files
7707 EOF
7708
7709     my ($last, @lcomm);
7710     $last = '';
7711     foreach my $iter (sort ((@common_files, @common_sometimes)))
7712     {
7713         push (@lcomm, $iter) unless $iter eq $last;
7714         $last = $iter;
7715     }
7716
7717     my ($one, $two, $three, $four, $max);
7718     print "\nFiles which are automatically distributed, if found:\n";
7719     format USAGE_FORMAT =
7720   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<
7721   $one,               $two,               $three,             $four
7722 .
7723     $~ = "USAGE_FORMAT";
7724     $max = int (($#lcomm + 1) / 4);
7725
7726     for (my $i = 0; $i < $max; ++$i)
7727     {
7728         $one = $lcomm[$i];
7729         $two = $lcomm[$max + $i];
7730         $three = $lcomm[2 * $max + $i];
7731         $four = $lcomm[3 * $max + $i];
7732         write;
7733     }
7734
7735     my $mod = ($#lcomm + 1) % 4;
7736     if ($mod != 0)
7737     {
7738         $one = $lcomm[$max];
7739         $two = ($mod > 1) ? $lcomm[2 * $max] : '';
7740         $three = ($mod > 2) ? $lcomm[3 * $max] : '';
7741         $four = ($mod > 3) ? $lcomm[4 * $max] : '';
7742         write;
7743     }
7744
7745     print "\nReport bugs to <bug-automake\@gnu.org>.\n";
7746
7747     exit 0;
7748 }
7749
7750 # &version ()
7751 # -----------
7752 # Print version information
7753 sub version ()
7754 {
7755   print <<EOF;
7756 automake (GNU $PACKAGE) $VERSION
7757 Written by Tom Tromey <tromey\@cygnus.com>.
7758
7759 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
7760 Free Software Foundation, Inc.
7761 This is free software; see the source for copying conditions.  There is NO
7762 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7763 EOF
7764   exit 0;
7765 }