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