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