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