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