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