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