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