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