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