ecfef69aa032415540338bbcda74e47386795384
[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 (C) 1994, 1995, 1996 Free Software Foundation, Inc.
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
14 # any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 # 02111-1307, USA.
25
26 # Originally written by David Mackenzie <djm@gnu.ai.mit.edu>.
27 # Perl reimplementation by Tom Tromey <tromey@cygnus.com>.
28
29
30 # Parameters set by configure.  Not to be changed.  NOTE: assign
31 # VERSION as string so that eg version 0.30 will print correctly.
32 $VERSION = "@VERSION@";
33 $PACKAGE = "@PACKAGE@";
34 $prefix = "@prefix@";
35 $am_dir = "@datadir@/@PACKAGE@";
36 $TAR = "@TAR@";
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 $MACRO_PATTERN = "^([A-Za-z][A-Za-z0-9_]*)[ \t]*:?=[ \t]*(.*)\$";
44 $BOGUS_MACRO_PATTERN = "^([^ \t]*)[ \t]*:?=[ \t]*(.*)\$";
45 $GNITS_VERSION_PATTERN = "[0-9]+\\.[0-9]+([a-z]|\\.[0-9]+)?";
46
47 # Some regular expressions.  One reason to put them here is that it
48 # makes indentation work better in Emacs.
49 $AC_CONFIG_AUX_DIR_PATTERN = "AC_CONFIG_AUX_DIR\\(([^)]+)\\)";
50 $AM_INIT_AUTOMAKE_PATTERN = "AM_INIT_AUTOMAKE\\([^,]*,([^)]+)\\)";
51 $AM_PACKAGE_VERSION_PATTERN = "^\\s*\\[?([^]\\s]+)\\]?\\s*\$";
52 # Note that there is no AC_PATH_TOOL.  But we don't really care.
53 $AC_CHECK_PATTERN = "AC_(CHECK|PATH)_(PROG|PROGS|TOOL)\\(\\[?(\\w+)";
54 # Just check for alphanumeric in AC_SUBST.  If you do AC_SUBST(5),
55 # then too bad.
56 $AC_SUBST_PATTERN = "AC_SUBST\\(\\[?(\\w+)";
57
58 # Constants to define the "strictness" level.
59 $FOREIGN = 0;
60 $GNU = 1;
61 $GNITS = 2;
62
63 \f
64
65 # Variables global to entire run.
66
67 # TRUE if we should always generate Makefile.in.
68 $force_generation = 1;
69
70 # Strictness level as set on command line.
71 $default_strictness = $GNU;
72
73 # Name of strictness level, as set on command line.
74 $default_strictness_name = 'gnu';
75
76 # This is TRUE if GNU make specific automatic dependency generation
77 # code should be included in generated Makefile.in.
78 $cmdline_use_dependencies = 1;
79
80 # TRUE if in verbose mode.
81 $verbose = 0;
82
83 # This holds our (eventual) exit status.  We don't actually exit until
84 # we have processed all input files.
85 $exit_status = 0;
86
87 # From the Perl manual.
88 $symlink_exists = (eval 'symlink ("", "");', $@ eq '');
89
90 # TRUE if missing standard files should be installed.
91 $add_missing = 0;
92
93 # Files found by scanning configure.in for LIBOBJS.
94 %libsources = ();
95
96 # True if AM_C_PROTOTYPES appears in configure.in.
97 $am_c_prototypes = 0;
98
99 # Names used in AC_CONFIG_HEADER call.  $config_name is the actual
100 # (first) argument.  $config_header is the '.in' file.  Ordinarily the
101 # second is derived from the first, but they can be different if the
102 # weird "NAME:FILE" syntax is used.
103 $config_name = '';
104 $config_header = '';
105 # Line number at which AC_CONFIG_HEADER appears in configure.in.
106 $config_header_line = 0;
107
108 # Directory where output files go.  Actually, output files are
109 # relative to this directory.
110 $output_directory = '.';
111
112 # Relative location of top build directory.
113 $top_builddir = '';
114
115 # Absolute location of top build directory.
116 $build_directory = '';
117
118 # Name of srcdir as given in build directory's Makefile.  For
119 # dependencies only.
120 $srcdir_name = '';
121
122 # List of Makefile.am's to process, and their corresponding outputs.
123 @input_files = ();
124 %output_files = ();
125
126 # List of files in AC_OUTPUT without Makefile.am, and their outputs.
127 @other_input_files = ();
128 # Line number at which AC_OUTPUT seen.
129 $ac_output_line = 0;
130
131 # List of directories to search for configure-required files.  This
132 # can be set by AC_CONFIG_AUX_DIR.
133 @config_aux_path = ('.', '..', '../..');
134 $config_aux_dir = '';
135
136 # Whether AC_PROG_MAKE_SET has been seen in configure.in.
137 $seen_make_set = 0;
138
139 # Whether AM_GNU_GETTEXT has been seen in configure.in.
140 $seen_gettext = 0;
141 # Line number at which AM_GNU_GETTEXT seen.
142 $ac_gettext_line = 0;
143
144 # Whether ALL_LINGUAS has been seen.
145 $seen_linguas = '';
146 # The actual text.
147 $all_linguas = '';
148 # Line number at which it appears.
149 $all_linguas_line = 0;
150
151 # 1 if AC_PROG_INSTALL seen, 2 if AM_PROG_INSTALL seen.
152 $seen_prog_install = 0;
153
154 # 1 if any scripts installed, 0 otherwise.
155 $scripts_installed = 0;
156
157 # Whether AC_PATH_XTRA has been seen in configure.in.
158 $seen_path_xtra = 0;
159
160 # TRUE if AC_DECL_YYTEXT was seen.
161 $seen_decl_yytext = 0;
162
163 # TRUE if we've seen AC_CANONICAL_(HOST|SYSTEM).  The presence of
164 # AC_CHECK_TOOL also sets this.
165 $seen_canonical = 0;
166
167 # TRUE if we've seen AC_ARG_PROGRAM.
168 $seen_arg_prog = 0;
169
170 # TRUE if we've seen AM_PROG_LIBTOOL.
171 $seen_libtool = 0;
172 $libtool_line = 0;
173
174 # Files installed by libtoolize.
175 @libtoolize_files = ('ltconfig', 'ltmain.sh', 'config.guess', 'config.sub');
176
177 # TRUE if we've seen AM_MAINTAINER_MODE.
178 $seen_maint_mode = 0;
179
180 # TRUE if we've seen PACKAGE and VERSION.
181 $seen_package = 0;
182 $seen_version = 0;
183
184 # Actual version we've seen.
185 $package_version = '';
186
187 # Line number where we saw version definition.
188 $package_version_line = 0;
189
190 # TRUE if we've seen AM_PATH_LISPDIR.
191 $seen_lispdir = 0;
192
193 # TRUE if we've seen AM_CYGWIN32.
194 $seen_cygwin32 = 0;
195
196 # Hash table of discovered configure substitutions.  Keys are names,
197 # values are meaningless.
198 %configure_vars = ();
199
200 # Charsets used by maintainer and in distribution.  MAINT_CHARSET is
201 # handled in a funny way: if seen in the top-level Makefile.am, it is
202 # used for every directory which does not specify a different value.
203 # The rationale here is that some directories (eg gettext) might be
204 # distributions of other packages, and thus require their own charset
205 # info.  However, the DIST_CHARSET must be the same for the entire
206 # package; it can only be set at top-level.
207 # FIXME: this yields bugs when rebuilding.  What to do?  Always
208 # read (and sometimes discard) top-level Makefile.am?
209 $maint_charset = '';
210 $dist_charset = 'utf8';         # recode doesn't support this yet.
211
212 # Name of input file ("Makefile.in") and output file ("Makefile.am").
213 # These have no directory components.
214 $am_file_name = '';
215 $in_file_name = '';
216
217 # TRUE if --cygnus seen.
218 $cygnus_mode = 0;
219
220 # Keys of this hash are names of dependency files to ignore.
221 %omit_dependencies = ();
222
223 # Map from obsolete macros to hints for new macros.
224 # FIXME complete the list so that there are no `0' hints.
225 %obsolete_macros =
226     (
227      'AC_FEATURE_CTYPE', "use \`AC_HEADER_STDC'",
228      'AC_FEATURE_ERRNO', "add \`strerror' to \`AC_REPLACE_FUNCS(...)'",
229      'AC_FEATURE_EXIT', 0,
230      'AC_SYSTEM_HEADER', 0,
231
232      # Note that we do not handle this one, because it is still run
233      # from AM_CONFIG_HEADER.  So we deal with it specially in
234      # scan_configure.
235      # 'AC_CONFIG_HEADER', "use \`AM_CONFIG_HEADER'",
236
237      'fp_C_PROTOTYPES', "use \`AM_C_PROTOTYPES'",
238      'fp_PROG_CC_STDC', "use \`AM_PROG_CC_STDC'",
239      'fp_PROG_INSTALL', "use \`AM_PROG_INSTALL'",
240      'fp_WITH_DMALLOC', "use \`AM_WITH_DMALLOC'",
241      'fp_WITH_REGEX', "use \`AM_WITH_REGEX'",
242      'gm_PROG_LIBTOOL', "use \`AM_PROG_LIBTOOL'",
243      'jm_MAINTAINER_MODE', "use \`AM_MAINTAINER_MODE'",
244      'md_TYPE_PTRDIFF_T', "use \`AM_TYPE_PTRDIFF_T'",
245      'ud_PATH_LISPDIR', "use \`AM_PATH_LISPDIR'",
246
247      # Now part of autoconf proper, under a different name.
248      'AM_FUNC_FNMATCH', "use \`AC_FUNC_FNMATCH'",
249      'fp_FUNC_FNMATCH', "use \`AC_FUNC_FNMATCH'",
250      'AM_SANITY_CHECK_CC', "automatically done by \`AC_PROG_CC'",
251
252 # These aren't quite obsolete.
253 #      'md_PATH_PROG',
254      );
255
256 # Regexp to match the above macros.
257 $obsolete_rx = '(' . join ('|', keys %obsolete_macros) . ')';
258
259 \f
260
261 &initialize_global_constants;
262
263 # Parse command line.
264 &parse_arguments (@ARGV);
265
266 # Do configure.in scan only once.
267 &scan_configure;
268
269 die "automake: no \`Makefile.am' found or specified\n"
270     if ! @input_files;
271
272 # Now do all the work on each file.
273 foreach $am_file (@input_files)
274 {
275     if (! -f ($am_file . '.am'))
276     {
277         &am_error ("\`" . $am_file . ".am' does not exist");
278     }
279     else
280     {
281         &generate_makefile ($output_files{$am_file}, $am_file);
282     }
283 }
284
285 if ($seen_prog_install <= $scripts_installed)
286 {
287     &am_conf_error (($scripts_installed ? 'AM_PROG_INSTALL' : 'AC_PROG_INSTALL')
288                     . " must be used in configure.in");
289     &keyed_aclocal_warning ('AM_PROG_INSTALL')
290         if $scripts_installed;
291 }
292
293 exit $exit_status;
294
295
296 ################################################################
297
298 # Parse command line.
299 sub parse_arguments
300 {
301     local (@arglist) = @_;
302
303     # Start off as gnu.
304     &set_strictness ('gnu');
305
306     while (@arglist)
307     {
308         if ($arglist[0] eq "--version")
309         {
310             print "automake (GNU $PACKAGE) $VERSION\n\n";
311             print "Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.\n";
312             print "This is free software; see the source for copying conditions.  There is NO\n";
313             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
314             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
315
316             exit 0;
317         }
318         elsif ($arglist[0] eq "--help")
319         {
320             &usage;
321         }
322         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
323         {
324             $am_dir = $1;
325         }
326         elsif ($arglist[0] eq '--amdir')
327         {
328             &require_argument (@arglist);
329             shift (@arglist);
330             $am_dir = $arglist[0];
331         }
332         elsif ($arglist[0] =~ /^--build-dir=(.+)$/)
333         {
334             # Must end in /.
335             $build_directory = $1 . '/';
336         }
337         elsif ($arglist[0] eq '--build-dir')
338         {
339             &require_argument (@arglist);
340             shift (@arglist);
341             # Must end in /.
342             $build_directory = $arglist[0] . '/';
343         }
344         elsif ($arglist[0] =~ /^--srcdir-name=(.+)$/)
345         {
346             $srcdir_name = $1;
347         }
348         elsif ($arglist[0] eq '--srcdir-name')
349         {
350             &require_argument (@arglist);
351             shift (@arglist);
352             $srcdir_name = $arglist[0];
353         }
354         elsif ($arglist[0] eq '--gnu')
355         {
356             &set_strictness ('gnu');
357         }
358         elsif ($arglist[0] eq '--gnits')
359         {
360             &set_strictness ('gnits');
361         }
362         elsif ($arglist[0] eq '--cygnus')
363         {
364             $cygnus_mode = 1;
365         }
366         elsif ($arglist[0] eq '--foreign')
367         {
368             &set_strictness ('foreign');
369         }
370         elsif ($arglist[0] eq '--include-deps' || $arglist[0] eq '-i')
371         {
372             $cmdline_use_dependencies = 0;
373         }
374         elsif ($arglist[0] eq '--no-force')
375         {
376             $force_generation = 0;
377         }
378         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
379         {
380             # Set output directory.
381             $output_directory = $1;
382         }
383         elsif ($arglist[0] eq '--output-dir' || $arglist[0] eq '-o')
384         {
385             &require_argument (@arglist);
386             shift (@arglist);
387             $output_directory = $arglist[0];
388         }
389         elsif ($arglist[0] eq '--add-missing' || $arglist[0] eq '-a')
390         {
391             $add_missing = 1;
392         }
393         elsif ($arglist[0] eq '--verbose' || $arglist[0] eq '-v')
394         {
395             $verbose = 1;
396         }
397         elsif ($arglist[0] eq '--')
398         {
399             # Stop option processing.
400             shift (@arglist);
401             push (@input_files, @arglist);
402             last;
403         }
404         elsif ($arglist[0] =~ /^-/)
405         {
406             die "automake: unrecognized option -- \`$arglist[0]'\nTry \`automake --help' for more information.\n";
407         }
408         else
409         {
410             # Handle $local:$input syntax.  Note that we only examine
411             # the first ":" file to see if it is automake input; the
412             # rest are just taken verbatim.  We still keep all the
413             # files around for dependency checking, however.
414             local ($local, $input, @rest) = split (/:/, $arglist[0]);
415             if (! $input)
416             {
417                 $input = $local;
418             }
419             else
420             {
421                 # Strip .in; later on .am is tacked on.  That is how
422                 # the automake input file is found.  Maybe not the
423                 # best way, but it is easy to explain.  FIXME: should
424                 # be error if .in is missing.
425                 $input =~ s/\.in$//;
426             }
427             push (@input_files, $input);
428             $output_files{$input} = join (':', ($local, @rest));
429         }
430
431         shift (@arglist);
432     }
433
434     # Take global strictness from whatever we currently have set.
435     $default_strictness = $strictness;
436     $default_strictness_name = $strictness_name;
437 }
438
439 # Ensure argument exists, or die.
440 sub require_argument
441 {
442     local ($arg, @arglist) = @_;
443     die "automake: no argument given for option \`$arg'\n"
444         if ! @arglist;
445 }
446
447 ################################################################
448
449 # Generate a Makefile.in given the name of the corresponding Makefile and
450 # the name of the file output by config.status.
451 sub generate_makefile
452 {
453     local ($output, $makefile) = @_;
454
455     ($am_file_name = $makefile) =~ s/^.*\///;
456     $in_file_name = $am_file_name . '.in';
457     $am_file_name .= '.am';
458
459     # $OUTPUT is encoded.  If it contains a ":" then the first element
460     # is the real output file, and all remaining elements are input
461     # files.  We don't scan or otherwise deal with these input file,
462     # other than to mark them as dependencies.  See scan_configure for
463     # details.
464     local (@secondary_inputs);
465     ($output, @secondary_inputs) = split (/:/, $output);
466
467     &initialize_per_input;
468     $relative_dir = &dirname ($output);
469     $am_relative_dir = &dirname ($makefile);
470
471     # At the toplevel directory, we might need config.guess, config.sub
472     # or libtool scripts (ltconfig and ltmain.sh).
473     if ($relative_dir eq '.')
474     {
475         # libtool requires some files.
476         &require_conf_file_with_conf_line ($libtool_line, $FOREIGN,
477                                            @libtoolize_files)
478             if $seen_libtool;
479
480         # AC_CANONICAL_HOST and AC_CANONICAL_SYSTEM need config.guess and
481         # config.sub.
482         &require_config_file ($FOREIGN, 'config.guess', 'config.sub')
483             if $seen_canonical;
484     }
485
486     # We still need Makefile.in here, because sometimes the `dist'
487     # target doesn't re-run automake.
488     if ($am_relative_dir eq $relative_dir)
489     {
490         # Only distribute the files if they are in the same subdir as
491         # the generated makefile.
492         &push_dist_common ($in_file_name, $am_file_name);
493     }
494     push (@sources, '$(SOURCES)')
495         if &variable_defined ('SOURCES');
496     push (@objects, '$(OBJECTS)')
497         if &variable_defined ('OBJECTS');
498
499     # This is always the default target.  This gives us freedom to do
500     # things in whatever order is convenient.  Note that we set up
501     # $output_header here so that we can insert some text just after
502     # the "default" target, but before any other targets.  In
503     # particular we want to support the .SUFFIX hack here; this is
504     # documented elsewhere.
505     $output_header = "default: all\n\n";
506     push (@phony, 'default');
507
508     &read_am_file ($makefile . '.am');
509     if (&handle_options)
510     {
511         # Fatal error.  Just return, so we can continue with next file.
512         return;
513     }
514
515     # Check first, because we might modify some state.
516     &check_cygnus;
517     &check_gnu_standards;
518     &check_gnits_standards;
519
520     &handle_configure ($output, $makefile, @secondary_inputs);
521     &handle_gettext;
522     &handle_libraries;
523     &handle_ltlibraries;
524     &handle_programs;
525     &handle_scripts;
526
527     &handle_built_sources;
528
529     # This must be run after all the sources are scanned.
530     &handle_yacc_lex_cxx;
531
532     # Re-init SOURCES and OBJECTS.  FIXME: other code shouldn't depend
533     # on this (but currently does).
534     $contents{'SOURCES'} = join (' ', @sources);
535     $contents{'OBJECTS'} = join (' ', @objects);
536
537     &handle_texinfo;
538     &handle_emacs_lisp;
539     &handle_man_pages;
540     &handle_data;
541     &handle_headers;
542     &handle_subdirs;
543     &handle_tags;
544     &handle_dist;
545     &handle_dependencies;
546     &handle_tests;
547     &handle_footer;
548     &handle_merge_targets;
549     &handle_installdirs;
550     &handle_clean;
551     &handle_phony;
552
553     &check_typos;
554
555     if (! -d ($output_directory . '/' . $am_relative_dir))
556     {
557         mkdir ($output_directory . '/' . $am_relative_dir, 0755);
558     }
559
560     local ($out_file) = $output_directory . '/' . $makefile . ".in";
561     if (! $force_generation && -e $out_file)
562     {
563         local ($am_time) = (stat ($makefile . '.am'))[9];
564         local ($in_time) = (stat ($out_file))[9];
565         # FIXME: how to do unsigned comparison?
566         if ($am_time < $in_time)
567         {
568             # No need to update.
569             return;
570         }
571     }
572
573     if (! open (GM_FILE, "> " . $out_file))
574     {
575         warn "automake: ${am_file}.in: cannot open: $!\n";
576         $exit_status = 1;
577         return;
578     }
579     print "automake: creating ", $makefile, ".in\n" if $verbose;
580
581     print GM_FILE $output_vars;
582     print GM_FILE $output_header;
583     print GM_FILE $output_rules;
584     print GM_FILE $output_trailer;
585
586     close (GM_FILE);
587 }
588
589 ################################################################
590
591 # Handle AUTOMAKE_OPTIONS variable.  Return 1 on error, 0 otherwise.
592 sub handle_options
593 {
594     return 0 if ! &variable_defined ('AUTOMAKE_OPTIONS');
595
596     foreach (&variable_value_as_list ('AUTOMAKE_OPTIONS'))
597     {
598         $options{$_} = 1;
599         if ($_ eq 'gnits' || $_ eq 'gnu' || $_ eq 'foreign')
600         {
601             &set_strictness ($_);
602         }
603         elsif (/ansi2knr/)
604         {
605             # An option like "../lib/ansi2knr" is allowed.  With no
606             # path prefix, we assume the required programs are in this
607             # directory.  We save the actual option for later.
608             $options{'ansi2knr'} = $_;
609         }
610         elsif ($_ eq 'no-installman' || $_ eq 'no-installinfo'
611                || $_ eq 'dist-shar' || $_ eq 'dist-zip'
612                || $_ eq 'dist-tarZ' || $_ eq 'dejagnu'
613                || $_ eq 'no-texinfo.tex')
614         {
615             # Explicitly recognize these.
616         }
617         elsif ($_ eq 'no-dependencies')
618         {
619             $use_dependencies = 0;
620         }
621         elsif (/([0-9]+)\.([0-9]+)/)
622         {
623             # Got a version number.  Note that alpha releases count as
624             # the next higher release.  Note also that we assume there
625             # will be a maximum of 100 minor releases for any given
626             # major release.
627             local ($rmajor, $rminor) = ($1, $2);
628             if ($VERSION !~ /([0-9]+)\.([0-9]+)([a-z])?/)
629             {
630                 print STDERR
631                     "automake: programming error: version is incorrect\n";
632                 exit 1;
633             }
634             local ($tmajor, $tminor) = ($1, $2);
635             # Handle alpha versions.
636             ++$tminor if defined $3;
637
638             if ($rmajor > $tmajor || ($rmajor == $tmajor && $rminor > $tminor))
639             {
640                 &am_line_error ('AUTOMAKE_OPTIONS',
641                                 "require version $_, only have $VERSION");
642                 return 1;
643             }
644         }
645         else
646         {
647             &am_line_error ('AUTOMAKE_OPTIONS',
648                             'option ', $_, 'not recognized');
649         }
650     }
651
652     return 0;
653 }
654
655 # Return object extension.  Just once, put some code into the output.
656 # Argument is the name of the output file
657 sub get_object_extension
658 {
659     local ($out) = @_;
660
661     # Always set this.
662     $dir_holds_sources = 1;
663
664     # Maybe require libtool library object files.
665     local ($l, $extension) = ('', 'o');
666     $l = 'l' if ($out =~ /^lib.*\.la$/);
667
668     if (! $included_generic_compile)
669     {
670         # Boilerplate.
671         local ($xform) = '';
672         if (&variable_defined ('CONFIG_HEADER'))
673         {
674             ($xform = &dirname ($contents{'CONFIG_HEADER'}))
675                 =~ s/(\W)/\\$1/g;
676             $xform = '-I' . $xform;
677         }
678         $xform = 's/\@CONFIG_INCLUDE_SPEC\@/' . $xform . '/go;';
679         $output_vars .= &file_contents_with_transform ($xform,
680                                                        'comp-vars');
681         $output_rules .= &file_contents ('compile');
682         &push_phony_cleaners ('compile');
683
684         # If using X, include some extra variable definitions.  NOTE
685         # we don't want to force these into CFLAGS or anything,
686         # because not all programs will necessarily use X.
687         if ($seen_path_xtra)
688         {
689             local ($var);
690             foreach $var ('X_CFLAGS', 'X_LIBS', 'X_EXTRA_LIBS', 'X_PRE_LIBS')
691             {
692                 &define_configure_variable ($var);
693             }
694         }
695
696         push (@suffixes, '.c', '.o');
697         push (@clean, 'compile');
698
699         $included_generic_compile = 1;
700     }
701
702     if ($seen_libtool && ! $included_libtool_compile)
703     {
704         # Output the libtool compilation rules.
705         $output_rules .= &file_contents ('libtool');
706         &push_phony_cleaners ('libtool');
707
708         push (@suffixes, '.lo');
709         push (@clean, 'libtool');
710
711         $included_libtool_compile = 1;
712     }
713
714     # Check for automatic de-ANSI-fication.
715     if (defined $options{'ansi2knr'})
716     {
717         $extension = '$o';
718         if (! $included_knr_compile)
719         {
720             if (! $am_c_prototypes)
721             {
722                 &am_line_error ('AUTOMAKE_OPTIONS',
723                                 "option \`ansi2knr' in use but \`AM_C_PROTOTYPES' not in configure.in");
724                 &keyed_aclocal_warning ('AM_C_PROTOTYPES');
725                 # Only give this error once.
726                 $am_c_prototypes = 1;
727             }
728
729             push (@suffixes, '._c', '._o');
730             push (@suffixes, '.l_o') if $seen_libtool;
731
732             # Only require ansi2knr files if they should appear in
733             # this directory.
734             if ($options{'ansi2knr'} eq 'ansi2knr')
735             {
736                 &require_file_with_line ('AUTOMAKE_OPTIONS', $FOREIGN,
737                                          'ansi2knr.c', 'ansi2knr.1');
738                 $output_rules .= &file_contents ('kr-extra');
739                 push (@clean, 'krextra');
740                 &push_phony_cleaners ('krextra');
741             }
742
743             &define_variable ('o', "\@U\@o");
744
745             # Make sure ansi2knr can be found: if no path specified,
746             # specify "./".
747             local ($apath) = $options{'ansi2knr'};
748             if ($apath =~ /\//)
749             {
750                 # Found in another directory.
751                 &define_variable ("ANSI2KNR", $apath);
752             }
753             else
754             {
755                 # Substitution from AM_C_PROTOTYPES.  This makes it be
756                 # built only when necessary.
757                 &define_configure_variable ('ANSI2KNR');
758                 # ansi2knr needs to be built before subdirs, so unshift it.
759                 unshift (@all, '$(ANSI2KNR)');
760             }
761
762             $output_rules .= &file_contents ('compile-kr');
763             $output_rules .= &file_contents ('clean-kr');
764
765             push (@clean, 'kr');
766             &push_phony_cleaners ('kr');
767
768             $included_knr_compile = 1;
769         }
770     }
771
772     return '.' . $l . $extension;
773 }
774
775 # Handle yacc and lex.
776 sub handle_yacc_lex_cxx
777 {
778     #
779     # First do yacc and lex.
780     #
781
782     local ($yacc_count) = scalar (keys %yacc_sources);
783     local ($lex_count) = scalar (keys %lex_sources);
784
785     if ($yacc_count)
786     {
787         local (%seen_suffix) = ();
788         foreach (keys %yacc_sources)
789         {
790             /(\..*)$/;
791             &output_yacc_build_rule ($1, $yacc_count > 1)
792                 if (! defined $seen_suffix{$1});
793             $seen_suffix{$1} = 1;
794         }
795
796         if (! defined $configure_vars{'YACC'})
797         {
798             &am_error ("yacc source seen but \`YACC' not defined in \`configure.in'");
799         }
800     }
801     if ($lex_count)
802     {
803         local (%seen_suffix) = ();
804         foreach (keys %lex_sources)
805         {
806             /(\..*)$/;
807             &output_lex_build_rule ($1, $lex_count > 1)
808                 if (! defined $seen_suffix{$1});
809             $seen_suffix{$1} = 1;
810         }
811
812         if (! defined $configure_vars{'LEX'})
813         {
814             &am_error ("lex source seen but \`LEX' not defined in \`configure.in'");
815         }
816         if (! $seen_decl_yytext)
817         {
818             &am_error ("lex source seen but \`AC_DECL_YYTEXT' not in \`configure.in'");
819         }
820     }
821
822     if ($yacc_count > 1 || $lex_count > 1)
823     {
824         # If there is more than one distinct yacc (resp lex) source
825         # file in a given directory, then the `interlock' program is
826         # required to allow parallel builds to work correctly.  FIXME:
827         # for now, no line number.
828         &require_config_file ($FOREIGN, 'interlock', 'ylwrap');
829         if ($config_aux_dir ne '.' && $config_aux_dir ne '')
830         {
831                 &define_variable ('INTERLOCK', $config_aux_dir . "/interlock");
832                 &define_variable ('YLWRAP', $config_aux_dir . "/ylwrap");
833         }
834         else
835         {
836                 &define_variable ('INTERLOCK', '$(srcdir)/interlock');
837                 &define_variable ('YLWRAP', '$(srcdir)/ylwrap');
838         }
839     }
840
841     #
842     # Handle libtool.
843     #
844     local ($ltcompile, $ltlink) = ('', '');
845     if ($seen_libtool)
846     {
847         &define_configure_variable ("LIBTOOL");
848         $ltcompile = '$(LIBTOOL) --mode=compile ';
849         $ltlink = '$(LIBTOOL) --mode=link ';
850     }
851
852     #
853     # Now handle C++.
854     #
855     local (@cxx_list) = keys %cxx_extensions;
856     local ($cxx_count) = scalar @cxx_list;
857     if ($cxx_count)
858     {
859         push (@suffixes, @cxx_list);
860
861         &define_configure_variable ("CXXFLAGS");
862         &define_variable ('CXXCOMPILE', '$(CXX) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS)');
863         &define_variable ('LTCXXCOMPILE',
864                           $ltcompile . '$(CXX) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS)')
865             if ($seen_libtool);
866
867         &define_variable ('CXXLINK', $ltlink . '$(CXX) $(LDFLAGS) -o $@');
868
869         local ($ext);
870         foreach $ext (@cxx_list)
871         {
872             $output_rules .= ("$ext.o:\n"
873                               . "\t\$(CXXCOMPILE) -c \$<\n");
874             $output_rules .= ("$ext.lo:\n"
875                               . "\t\$(LTCXXCOMPILE) -c \$<\n")
876                 if ($seen_libtool);
877         }
878
879         if (! defined $configure_vars{'CXX'})
880         {
881             &am_error ("C++ source seen but \`CXX' not defined in \`configure.in'");
882         }
883     }
884
885     #
886     # Handle some ansi2knr cleanup.
887     #
888     if (defined $options{'ansi2knr'} && keys %de_ansi_objects)
889     {
890         # Make all ._o files depend on ansi2knr.  Use a sneaky little
891         # hack to make it print nicely.
892         &pretty_print_rule ('', '', (sort keys %de_ansi_objects,
893                                      ':', '$(ANSI2KNR)'));
894     }
895
896     #
897     # Last, handle some C cleanup.
898     #
899     if ($seen_c_source)
900     {
901         &define_configure_variable ('CFLAGS');
902         &define_variable ('COMPILE',
903                           '$(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)');
904         &define_variable ('LTCOMPILE',
905                           $ltcompile .
906                           '$(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)')
907             if ($seen_libtool);
908         &define_variable ('LINK', $ltlink . '$(CC) $(LDFLAGS) -o $@');
909
910         if (! defined $configure_vars{'CC'})
911         {
912             &am_line_error ($seen_c_source,
913                             "C source seen but \`CC' not defined in \`configure.in'");
914         }
915     }
916 }
917
918
919 # Output a rule to build from a YACC source.  The output from YACC is
920 # compiled with C or C++, depending on the extension of the YACC file.
921 sub output_yacc_build_rule
922 {
923     local ($yacc_suffix, $use_interlock) = @_;
924     local ($c_suffix);
925
926     ($c_suffix = $yacc_suffix) =~ tr/y/c/;
927     push (@suffixes, $yacc_suffix);
928     $output_rules .= "$yacc_suffix$c_suffix:\n\t";
929
930     if ($use_interlock)
931     {
932         $output_rules .= '$(SHELL) $(INTERLOCK) =yacclockdir $(YLWRAP)'
933             . ' "$(YACC)" y.tab.c $*' . $c_suffix 
934             . ' y.tab.h $*.h -- $(YFLAGS) $<';
935     }
936     else
937     {
938         $output_rules .= ('$(YACC) $(YFLAGS) $< && mv y.tab.c $@' . "\n"
939                           . "\tif test -f y.tab.h; then \\\n"
940                           . "\tif cmp -s y.tab.h \$*.h; then rm -f y.tab.h; else mv y.tab.h \$*.h; fi; \\\n"
941                           . "\telse :; fi");
942     }
943     $output_rules .= "\n";
944 }
945
946 sub output_lex_build_rule
947 {
948     local ($lex_suffix, $use_interlock) = @_;
949     local ($c_suffix);
950
951     ($c_suffix = $lex_suffix) =~ tr/l/c/;
952     push (@suffixes, $lex_suffix);
953     &define_configure_variable ('LEX_OUTPUT_ROOT');
954     &define_configure_variable ('LEXLIB');
955     $output_rules .= "$lex_suffix$c_suffix:\n\t";
956     
957     if ($use_interlock)
958     { 
959         # is the $@ correct here?  If so, why not use it in the
960         # interlock build rule for yacc above?
961         $output_rules .= '$(SHELL) $(INTERLOCK) =lexlockdir $(YLWRAP)'
962             . ' "$(LEX)" $(LEX_OUTPUT_ROOT).c $@ -- $(LFLAGS) $<';
963     }
964     else
965     {
966         $output_rules .= '$(LEX) $(LFLAGS) $< && mv $(LEX_OUTPUT_ROOT).c $@';
967     }
968     $output_rules .= "\n";
969 }
970
971
972 # Check to make sure a source defined in LIBOBJS is not explicitly
973 # mentioned.  This is a separate function (as opposed to being inlined
974 # in handle_source_transform) because it isn't always appropriate to
975 # do this check.
976 sub check_libobjs_sources
977 {
978     local ($one_file, $unxformed) = @_;
979
980     local ($prefix, $file, @files);
981     foreach $prefix ('', 'EXTRA_')
982     {
983         if (&variable_defined ($prefix . $one_file . '_SOURCES'))
984         {
985             @files = &variable_value_as_list ($prefix
986                                               . $one_file . '_SOURCES');
987         }
988         elsif ($prefix eq '')
989         {
990             @files = ($unxformed . '.c');
991         }
992         else
993         {
994             next;
995         }
996
997         foreach $file (@files)
998         {
999             if (defined $libsources{$file})
1000             {
1001                 &am_line_error ($prefix . $one_file . '_SOURCES',
1002                                 "automatically discovered file \`$file' should not be explicitly mentioned");
1003             }
1004         }
1005     }
1006 }
1007
1008 # Does much of the actual work for handle_source_transform.
1009 # Arguments are:
1010 #   list of source files to transform
1011 # Result is a list
1012 #   first element is name of linker to use (empty string for default linker)
1013 #   remaining elements are names of `.o's
1014 sub handle_single_transform_list
1015 {
1016     local (@files) = @_;
1017     local ($linker) = '';
1018     local (@result) = ();
1019     local ($nonansi_obj) = $obj;
1020     $nonansi_obj =~ s/_//g;
1021     if (length (@files))
1022     {
1023         # Turn sources into objects.
1024         foreach (@files)
1025         {
1026             # Skip header files, including C++-ish ones.  The list
1027             # of C++ header extensions comes from Emacs 19.32
1028             # etags.
1029             next if /\.[hH]$/;
1030             next if /\.hxx$/;
1031             next if /\.h\+\+$/;
1032             next if /\.hh$/;
1033             next if /\.hpp$/;
1034             # Skip things that look like configure substitutions.
1035             next if /^\@.*\@$/;
1036
1037             # Include appropriate file for lex or yacc source in
1038             # distribution.  If the extension is the regular '.y' or
1039             # '.l', we assume C compilation, and the generated file
1040             # has exension .c.  Otherwise, we compile with C++, and
1041             # make the following association: (yy -> cc, y++ -> c++, 
1042             # yxx -> cxx), similarly for .ll, etc.
1043             if (/^(.*)\.(y|yy|y\+\+|yxx)$/)
1044             {
1045                 # Yacc source.
1046                 local ($ext) = $2;
1047                 $ext =~ tr/y/c/;
1048                 &push_dist_common ("$1.$ext");
1049                 $yacc_sources{$_} = 1;
1050             }
1051             elsif (/^(.*)\.(l|ll|l\+\+|lxx)$/)
1052             {
1053                 # Lex source.
1054                 local ($ext) = $2;
1055                 $ext =~ tr/l/c/;
1056                 &push_dist_common ("$1.$ext");
1057                 $lex_sources{$_} = 1;
1058             }
1059
1060             # Transform source files into .o files.  List of C++
1061             # extensions comes from Emacs 19.34 etags.
1062             if (s/\.(c\+\+|cc|cpp|cxx|C)$/$nonansi_obj/)
1063             {
1064                 $cxx_extensions{'.' . $1} = 1;
1065                 $linker = 'CXXLINK';
1066             }
1067             elsif (s/\.(yy|y\+\+|yxx|ll|l\+\+|lxx)$/$nonansi_obj/)
1068             {
1069                 # Compiling lex or yacc with C++
1070                 local ($ext) = $1;
1071                 $ext =~ tr/ly/cc/;
1072                 $cxx_extensions{".$ext"} = 1;
1073                 $linker = 'CXXLINK';
1074             }
1075             elsif (s/\.([Ff]\\|f90\\|for)$/$nonansi_obj/)
1076             {
1077                 # FORTRAN support.  FIXME: not finished.
1078             }
1079             elsif (s/\.[sS]$/$nonansi_obj/)
1080             {
1081                 # .s is assembly.  Just rewrite it.  FIXME: not finished.
1082             }
1083             elsif (s/\.[cly]$/._o/)
1084             {
1085                 # .c is C.  .l is lex.  .y is yacc.
1086
1087                 # Note: first we rewrite (eg) foo.c to foo._o and push
1088                 # the file onto the list of objects that require
1089                 # ansi2knr.  Then we rewrite ._o to $obj; $obj can be
1090                 # simply `.o' if deansification is not really
1091                 # required.
1092                 $de_ansi_objects{$_} = 1;
1093                 s/\._o$/$obj/;
1094                 $seen_c_source = -1 unless $seen_c_source;
1095             }
1096             else
1097             {
1098                 # No error message here.  Used to have one, but it was
1099                 # very unpopular.
1100                 next;
1101             }
1102
1103             push (@result, $_);
1104
1105             # Transform .o or $o file into .P file (for automatic
1106             # dependency code).
1107             s/$objpat$/.P/g;
1108             $dep_files{'.deps/' . $_} = 1;
1109         }
1110     }
1111
1112     return ($linker, @result);
1113 }
1114
1115 # Handle SOURCE->OBJECT transform for one program or library.
1116 # Arguments are:
1117 #   canonical (transformed) name of object to build
1118 #   actual name of object to build
1119 #   object extension (ie either `.o' or `$o'.
1120 # Return result is name of linker variable that must be used.
1121 # Empty return means just use `LINK'.
1122 sub handle_source_transform
1123 {
1124     # one_file is canonical name.  unxformed is given name.  obj is
1125     # object extension.
1126     local ($one_file, $unxformed, $obj) = @_;
1127     local ($objpat) = $obj;
1128     $objpat =~ s/(\W)/\\$1/g;
1129     # Handle explicit `.o' as well as whatever we're passed.
1130     $objpat = '(' . $objpat . "|\\.o)";
1131
1132     local ($linker) = '';
1133
1134     if (&variable_defined ($one_file . "_OBJECTS"))
1135     {
1136         &am_line_error ($one_file . '_OBJECTS',
1137                         $one_file . '_OBJECTS', 'should not be defined');
1138         # No point in continuing.
1139         return;
1140     }
1141
1142     local (@files, @result, $prefix, $temp);
1143     foreach $prefix ('', 'EXTRA_')
1144     {
1145         @files = ();
1146         if (&variable_defined ($prefix . $one_file . "_SOURCES"))
1147         {
1148             push (@sources, '$(' . $prefix . $one_file . "_SOURCES)");
1149             push (@objects, '$(' . $prefix . $one_file . "_OBJECTS)")
1150                 unless $prefix eq 'EXTRA_';
1151             @files = &variable_value_as_list ($prefix
1152                                               . $one_file . "_SOURCES");
1153         }
1154         elsif ($prefix eq '')
1155         {
1156             &define_variable ($one_file . "_SOURCES", $unxformed . ".c");
1157             push (@sources, $unxformed . '.c');
1158             push (@objects, $unxformed . $obj);
1159             push (@files, $unxformed . '.c');
1160         }
1161
1162         ($temp, @result) = &handle_single_transform_list (@files);
1163         $linker = $temp if $linker eq '';
1164         &define_pretty_variable ($one_file . "_OBJECTS", @result)
1165             unless $prefix eq 'EXTRA_';
1166     }
1167
1168     if (&variable_defined ('CONFIG_HEADER'))
1169     {
1170         $output_rules .= ('$(' . $one_file . "_OBJECTS): "
1171                           . $contents{'CONFIG_HEADER'} . "\n");
1172     }
1173
1174     return $linker;
1175 }
1176
1177 # Handle the BUILT_SOURCES variable.
1178 sub handle_built_sources
1179 {
1180     return unless &variable_defined ('BUILT_SOURCES');
1181     push (@all, '$(BUILT_SOURCES)');
1182
1183     local (@sources) = &variable_value_as_list ('BUILT_SOURCES');
1184     local ($s);
1185     foreach $s (@sources)
1186     {
1187         if (/^\@.*\@$/)
1188         {
1189             # FIXME: is this really the right thing to do?
1190             &am_line_error ('BUILT_SOURCES', "\`BUILT_SOURCES' should not contain a configure substitution");
1191             last;
1192         }
1193     }
1194
1195     # We don't care about the return value of this function.  We just
1196     # want to make sure to update %dep_files with the contents of
1197     # BUILT_SOURCES.
1198     &handle_single_transform_list (@sources);
1199 }
1200
1201 # Special-case @ALLOCA@ and @LIBOBJS@ in _LDADD or _LIBADD variables.
1202 # Also, generate _DEPENDENCIES variable if appropriate.
1203 # Arguments are:
1204 #   transformed name of object being built, or empty string if no object
1205 #   name of _LDADD/_LIBADD-type variable to examine
1206 #   boolean (lex_seen) which is true if a lex source file was seen in this
1207 #     object.  valid only for LDADDs, not LIBADDs.  If set, LEXLIB
1208 #     must be in LDADD.
1209 # Returns 1 if LIBOBJS seen, 0 otherwise.
1210 sub handle_lib_objects
1211 {
1212     local ($xname, $var, $lex_seen) = @_;
1213
1214     die "automake: programming error 1 in handle_lib_objects\n"
1215         if ! &variable_defined ($var);
1216
1217     die "automake: programming error 2 in handle_lib_objects\n"
1218         if $lex_seen && $var =~ /LIBADD/;
1219
1220     # We recognize certain things that are commonly put in LIBADD or
1221     # LDADD.
1222     local ($lsearch);
1223     local (@dep_list) = ();
1224
1225     # If no lex source seen, just assume this is ok.
1226     local ($lex_ok) = $lex_seen ? 0 : 1;
1227
1228     local ($seen_libobjs) = 0;
1229     local ($flagvar) = 0;
1230
1231     foreach $lsearch (&variable_value_as_list ($var))
1232     {
1233         # Skip -lfoo and -Ldir; these are explicitly allowed.
1234         next if $lsearch =~ /^-[lL]/;
1235         if (! $flagvar && $lsearch =~ /^-/)
1236         {
1237             if ($var =~ /^(.*)LDADD$/)
1238             {
1239                 &am_line_error ($var, "linker flags such as \`$lsearch' belong in \`${1}LDFLAGS");
1240             }
1241             else
1242             {
1243                 # Only get this error once.
1244                 $flagvar = 1;
1245                 &am_line_error ($var, "you cannot use linker flags such as \`$lsearch' in \`$var'");
1246             }
1247         }
1248
1249         # Assume we have a file of some sort, and push it onto the
1250         # dependency list.  Autoconf substitutions are not pushed;
1251         # rarely is a new dependency substituted into (eg) foo_LDADD
1252         # -- but "bad things (eg -lX11) are routinely substituted.
1253         # Note that LIBOBJS and ALLOCA are exceptions to this rule,
1254         # and handled specially below.
1255         push (@dep_list, $lsearch)
1256             unless $lsearch =~ /^\@.*\@$/;
1257
1258         # Automatically handle @LIBOBJS@ and @ALLOCA@.  Basically this
1259         # means adding entries to dep_files.
1260         if ($lsearch =~ /^\@(LT)?LIBOBJS\@$/)
1261         {
1262             push (@dep_list, $lsearch);
1263             $seen_libobjs = 1;
1264             if (! keys %libsources)
1265             {
1266                 &am_line_error ($var, "\@$1" . "LIBOBJS\@ seen but never set in \`configure.in'");
1267             }
1268
1269             local ($iter, $rewrite);
1270             foreach $iter (keys %libsources)
1271             {
1272                 if ($iter =~ /\.[cly]$/)
1273                 {
1274                     $seen_c_source = $var;
1275                 }
1276
1277                 if ($iter =~ /\.h$/)
1278                 {
1279                     &require_file_with_line ($var, $FOREIGN, $iter);
1280                 }
1281                 elsif ($iter ne 'alloca.c')
1282                 {
1283                     ($rewrite = $iter) =~ s/\.c$/.P/;
1284                     $dep_files{'.deps/' . $rewrite} = 1;
1285                     &require_file_with_line ($var, $FOREIGN, $iter);
1286                 }
1287             }
1288         }
1289         elsif ($lsearch =~ /^\@(LT)?ALLOCA\@$/)
1290         {
1291             push (@dep_list, $lsearch);
1292             &am_line_error ($var,
1293                             "\@$1" . "ALLOCA\@ seen but \`AC_FUNC_ALLOCA' not in \`configure.in'")
1294                 if ! defined $libsources{'alloca.c'};
1295             $dep_files{'.deps/alloca.P'} = 1;
1296             &require_file_with_line ($var, $FOREIGN, 'alloca.c');
1297             $seen_c_source = $var;
1298         }
1299         elsif ($lsearch eq '@LEXLIB@')
1300         {
1301             # FIXME: variable_value_as_list requires us to force
1302             # @LEXLIB@ here, where we'd really prefer $(LEXLIB).
1303             # Nasty -- this will have to wait until many cleanups are
1304             # made, I think.
1305             $lex_ok = 1;
1306         }
1307     }
1308
1309     if (! $lex_ok)
1310     {
1311         &am_line_error ($var, 'lex source file used without @LEXLIB@');
1312     }
1313
1314     if ($xname ne '' && ! &variable_defined ($xname . '_DEPENDENCIES'))
1315     {
1316         &define_pretty_variable ($xname . '_DEPENDENCIES', @dep_list);
1317     }
1318
1319     return $seen_libobjs;
1320 }
1321
1322 # Handle C programs.
1323 sub handle_programs
1324 {
1325     local (@proglist) = &am_install_var ('-clean',
1326                                          'progs', 'PROGRAMS',
1327                                          'bin', 'sbin', 'libexec', 'pkglib',
1328                                          'noinst', 'check');
1329     return if ! @proglist;
1330
1331     # If a program is installed, this is required.  We only want this
1332     # error to appear once.
1333     &am_conf_error ("AC_ARG_PROGRAM must be used in configure.in")
1334         unless $seen_arg_prog;
1335     $seen_arg_prog = 1;
1336
1337     local ($one_file, $xname, $munge);
1338
1339     local ($seen_libobjs) = 0;
1340     foreach $one_file (@proglist)
1341     {
1342         local ($obj) = &get_object_extension ($one_file);
1343
1344         # Canonicalize names.
1345         ($xname = $one_file) =~ tr/A-Za-z0-9_/_/c;
1346         if ($xname ne $one_file)
1347         {
1348             local ($xt);
1349             foreach $xt ('_LDADD', '_LDFLAGS', '_SOURCES', '_OBJECTS', '_DEPENDENCIES')
1350             {
1351                 &am_line_error ($one_file . $xt,
1352                                 "invalid variable \`" . $one_file . $xt
1353                                 . "'; should be \`" . $xname . $xt . "'")
1354                     if &variable_defined ($one_file . $xt);
1355             }
1356         }
1357
1358         # FIXME: Using a trick to figure out if any lex sources appear
1359         # in our program; should use some cleaner method.
1360         local ($lex_num) = scalar (keys %lex_sources);
1361         local ($linker) = &handle_source_transform ($xname, $one_file, $obj);
1362         local ($lex_file_seen) = (scalar (keys %lex_sources) > $lex_num);
1363
1364         local ($xt) = '';
1365         if (&variable_defined ($xname . "_LDADD"))
1366         {
1367             if (&handle_lib_objects ($xname, $xname . '_LDADD',
1368                                      $lex_file_seen))
1369             {
1370                 $seen_libobjs = 1;
1371             }
1372             $lex_file_seen = 0;
1373             $xt = '_LDADD';
1374         }
1375         else
1376         {
1377             # User didn't define prog_LDADD override.  So do it.
1378             &define_variable ($xname . '_LDADD', '$(LDADD)');
1379
1380             # This does a bit too much work.  But we need it to
1381             # generate _DEPENDENCIES when appropriate.
1382             if (&variable_defined ('LDADD'))
1383             {
1384                 if (&handle_lib_objects ($xname, 'LDADD', $lex_file_seen))
1385                 {
1386                     $seen_libobjs = 1;
1387                 }
1388                 $lex_file_seen = 0;
1389             }
1390             $xt = '_SOURCES'
1391         }
1392
1393         if (! &variable_defined ($xname . '_LDFLAGS'))
1394         {
1395             # Define the prog_LDFLAGS variable.
1396             &define_variable ($xname . '_LDFLAGS', '');
1397         }
1398
1399         if ($lex_file_seen)
1400         {
1401             &am_line_error ($xname . $xt,
1402                             'lex source file used without @LEXLIB@');
1403         }
1404
1405         # Determine program to use for link.
1406         local ($xlink);
1407         if (&variable_defined ($xname . '_LINK'))
1408         {
1409             $xlink = $xname . '_LINK';
1410         }
1411         else
1412         {
1413             $xlink = $linker ? $linker : 'LINK';
1414         }
1415
1416         local ($cygxform);
1417         if (! $seen_cygwin32)
1418         {
1419             $cygxform = 's/^CYGWIN.*$//; s/^NOTCYGWIN//;';
1420         }
1421         else
1422         {
1423             $cygxform = 's/^NOTCYGWIN.*$//; s/^CYGWIN//;';
1424         }
1425
1426         $output_rules .=
1427             &file_contents_with_transform
1428                 ('s/\@PROGRAM\@/' . $one_file . '/go;'
1429                  . 's/\@XPROGRAM\@/' . $xname . '/go;'
1430                  . 's/\@XLINK\@/' . $xlink . '/go;'
1431                  . $cygxform,
1432                  'program');
1433     }
1434
1435     if (&variable_defined ('LDADD') && &handle_lib_objects ('', 'LDADD', 0))
1436     {
1437         $seen_libobjs = 1;
1438     }
1439
1440     if ($seen_libobjs)
1441     {
1442         foreach $one_file (@proglist)
1443         {
1444             # Canonicalize names.
1445             ($xname = $one_file) =~ tr/A-Za-z0-9_/_/c;
1446
1447             if (&variable_defined ($xname . '_LDADD'))
1448             {
1449                 &check_libobjs_sources ($xname, $xname . '_LDADD');
1450             }
1451             elsif (&variable_defined ('LDADD'))
1452             {
1453                 &check_libobjs_sources ($xname, 'LDADD');
1454             }
1455         }
1456     }
1457 }
1458
1459
1460 # Handle libraries.
1461 sub handle_libraries
1462 {
1463     local (@liblist) = &am_install_var ('-clean',
1464                                         'libs', 'LIBRARIES',
1465                                         'lib', 'pkglib', 'noinst', 'check');
1466     return if ! @liblist;
1467
1468     local (%valid) = &am_primary_prefixes ('LIBRARIES', 'lib', 'pkglib',
1469                                            'noinst', 'check');
1470     if (! defined $configure_vars{'RANLIB'})
1471     {
1472         local ($key);
1473         foreach $key (keys %valid)
1474         {
1475             if (&variable_defined ($key . '_LIBRARIES'))
1476             {
1477                 &am_line_error ($key . '_LIBRARIES', "library used but \`RANLIB' not defined in \`configure.in'");
1478                 # Only get this error once.
1479                 $configure_vars{'RANLIB'} = 1;
1480                 last;
1481             }
1482         }
1483     }
1484
1485     local ($onelib);
1486     local ($munge);
1487     local ($xlib);
1488     local ($seen_libobjs) = 0;
1489     foreach $onelib (@liblist)
1490     {
1491         # Check that the library fits the standard naming convention.
1492         if ($onelib !~ /^lib.*\.a$/)
1493         {
1494             # FIXME this should only be a warning for foreign packages
1495             # FIXME should put line number here.  That means mapping
1496             # from library name back to variable name.
1497             &am_error ("\`$onelib' is not a standard library name");
1498         }
1499
1500         local ($obj) = &get_object_extension ($onelib);
1501
1502         # Canonicalize names.
1503         ($xlib = $onelib) =~ tr/A-Za-z0-9_/_/c;
1504         if ($xlib ne $onelib)
1505         {
1506             local ($xt);
1507             foreach $xt ('_LIBADD', '_SOURCES', '_OBJECTS', '_DEPENDENCIES')
1508             {
1509                 &am_line_error ($onelib . $xt,
1510                                 "invalid variable \`" . $onelib . $xt
1511                                 . "'; should be \`" . $xlib . $xt . "'")
1512                     if &variable_defined ($onelib . $xt);
1513             }
1514         }
1515
1516         if (&variable_defined ($xlib . '_LIBADD'))
1517         {
1518             if (&handle_lib_objects ($xlib, $xlib . '_LIBADD', 0))
1519             {
1520                 $seen_libobjs = 1;
1521             }
1522         }
1523         else
1524         {
1525             # Generate support for conditional object inclusion in
1526             # libraries.
1527             &define_variable ($xlib . "_LIBADD", '');
1528         }
1529
1530         &handle_source_transform ($xlib, $onelib, $obj);
1531
1532         $output_rules .=
1533             &file_contents_with_transform ('s/\@LIBRARY\@/' . $onelib . '/go;'
1534                                            . 's/\@XLIBRARY\@/'
1535                                            . $xlib . '/go;',
1536                                            'library');
1537     }
1538
1539     if ($seen_libobjs)
1540     {
1541         foreach $onelib (@liblist)
1542         {
1543             # Canonicalize names.
1544             ($xlib = $onelib) =~ tr/A-Za-z0-9_/_/c;
1545             if (&variable_defined ($xlib . '_LIBADD'))
1546             {
1547                 &check_libobjs_sources ($xlib, $xlib . '_LIBADD');
1548             }
1549         }
1550     }
1551
1552     &define_variable ('AR', 'ar');
1553     &define_configure_variable ('RANLIB');
1554 }
1555
1556 # Handle shared libraries.
1557 sub handle_ltlibraries
1558 {
1559     local (@liblist) = &am_install_var ('-clean',
1560                                         'ltlib', 'LTLIBRARIES',
1561                                         'lib', 'pkglib');
1562     return if ! @liblist;
1563
1564     local (%instdirs);
1565     local (%valid) = &am_primary_prefixes ('LTLIBRARIES', 'lib', 'pkglib');
1566
1567     local ($key);
1568     foreach $key (keys %valid)
1569     {
1570         if (&variable_defined ($key . '_LTLIBRARIES'))
1571         {
1572             if (!$seen_libtool)
1573             {
1574                 &am_line_error ($key . '_LTLIBRARIES', "library used but \`LIBTOOL' not defined in \`configure.in'");
1575                 # Only get this error once.
1576                 $configure_vars{'LIBTOOL'} = 1;
1577                 $seen_libtool = 1;
1578             }
1579
1580             # Get the installation directory of each library.
1581             next if ($key eq 'EXTRA');
1582             for (&variable_value_as_list ($key . '_LTLIBRARIES'))
1583             {
1584                 if ($instdirs{$_})
1585                 {
1586                     &am_error ("\`$_' is already going to be installed in \`$instdirs{$_}'");
1587                 }
1588                 else
1589                 {
1590                     $instdirs{$_} = $key;
1591                 }
1592             }
1593         }
1594     }
1595
1596     local ($onelib);
1597     local ($munge);
1598     local ($xlib);
1599     local ($seen_libobjs) = 0;
1600     foreach $onelib (@liblist)
1601     {
1602         # Check that the library fits the standard naming convention.
1603         if ($onelib !~ /^lib.*\.la$/)
1604         {
1605             # FIXME this should only be a warning for foreign packages
1606             # FIXME should put line number here.  That means mapping
1607             # from library name back to variable name.
1608             &am_error ("\`$onelib' is not a standard libtool library name");
1609         }
1610
1611         local ($obj) = &get_object_extension ($onelib);
1612
1613         # Canonicalize names.
1614         ($xlib = $onelib) =~ tr/A-Za-z0-9_/_/c;
1615         if ($xlib ne $onelib)
1616         {
1617             local ($xt);
1618             foreach $xt ('_LIBADD', '_LDFLAGS', '_SOURCES', '_OBJECTS', '_DEPENDENCIES')
1619             {
1620                 &am_line_error ($onelib . $xt,
1621                                 "invalid variable \`" . $onelib . $xt
1622                                 . "'; should be \`" . $xlib . $xt . "'")
1623                     if &variable_defined ($onelib . $xt);
1624             }
1625         }
1626
1627         if (! &variable_defined ($xlib . '_LDFLAGS'))
1628         {
1629             # Define the lib_LDFLAGS variable.
1630             &define_variable ($xlib . '_LDFLAGS', '');
1631         }
1632
1633         if (&variable_defined ($xlib . '_LIBADD'))
1634         {
1635             if (&handle_lib_objects ($xlib, $xlib . '_LIBADD', 0))
1636             {
1637                 $seen_libobjs = 1;
1638             }
1639         }
1640         else
1641         {
1642             # Generate support for conditional object inclusion in
1643             # libraries.
1644             &define_variable ($xlib . "_LIBADD", '');
1645         }
1646
1647         local ($linker) = &handle_source_transform ($xlib, $onelib, $obj);
1648
1649         # Determine program to use for link.
1650         local ($xlink);
1651         if (&variable_defined ($xlib . '_LINK'))
1652         {
1653             $xlink = $xlib . '_LINK';
1654         }
1655         else
1656         {
1657             $xlink = $linker ? $linker : 'LINK';
1658         }
1659
1660
1661         $output_rules .=
1662             &file_contents_with_transform ('s/\@LTLIBRARY\@/'
1663                                            . $onelib . '/go;'
1664                                            . 's/\@XLTLIBRARY\@/'
1665                                            . $xlib . '/go;'
1666                                            . 's/\@DIR\@/'
1667                                            . $instdirs{$onelib} . '/go;'
1668                                            . 's/\@XLINK\@/' . $xlink . '/go;',
1669                                            'ltlibrary');
1670     }
1671
1672     if ($seen_libobjs)
1673     {
1674         foreach $onelib (@liblist)
1675         {
1676             # Canonicalize names.
1677             ($xlib = $onelib) =~ tr/A-Za-z0-9_/_/c;
1678             if (&variable_defined ($xlib . '_LIBADD'))
1679             {
1680                 &check_libobjs_sources ($xlib, $xlib . '_LIBADD');
1681             }
1682         }
1683     }
1684 }
1685
1686 # See if any _SOURCES variable were misspelled.  Also, make sure that
1687 # EXTRA_ variables don't contain configure substitutions.
1688 sub check_typos
1689 {
1690     local ($varname, $primary);
1691     foreach $varname (keys %contents)
1692     {
1693         foreach $primary ('_SOURCES', '_LIBADD', '_LDADD', '_LDFLAGS', '_DEPENDENCIES')
1694         {
1695             if ($varname =~ /$primary$/ && ! $content_seen{$varname})
1696             {
1697                 &am_line_error ($varname,
1698                                 "invalid unused variable name: \`$varname'");
1699             }
1700         }
1701     }
1702 }
1703
1704 # Handle scripts.
1705 sub handle_scripts
1706 {
1707     # NOTE we no longer automatically clean SCRIPTS, because it is
1708     # useful to sometimes distribute scripts verbatim.  This happens
1709     # eg in Automake itself.
1710     &am_install_var ('scripts', 'SCRIPTS',
1711                      'bin', 'sbin', 'libexec', 'pkgdata',
1712                      'noinst', 'check');
1713
1714     # Set $scripts_installed if appropriate.  Make sure we only find
1715     # scripts which are actually installed -- this is why we can't
1716     # simply use the return value of am_install_var.
1717     local (%valid) = &am_primary_prefixes ('SCRIPTS', 'bin', 'sbin',
1718                                            'libexec', 'pkgdata',
1719                                            'noinst', 'check');
1720     local ($key);
1721     foreach $key (keys %valid)
1722     {
1723         if ($key ne 'noinst'
1724             && $key ne 'check'
1725             && &variable_defined ($key . '_SCRIPTS'))
1726         {
1727             $scripts_installed = 1;
1728             # push (@check_tests, 'check-' . $key . 'SCRIPTS');
1729         }
1730     }
1731
1732     if ($scripts_installed)
1733     {
1734         # If a program is installed, this is required.  We only want this
1735         # error to appear once.
1736         &am_conf_error ("AC_ARG_PROGRAM must be used in configure.in")
1737             unless $seen_arg_prog;
1738         $seen_arg_prog = 1;
1739     }
1740 }
1741
1742 # Search a file for a "version.texi" Texinfo include.  Return the name
1743 # of the include file if found, or the empty string if not.  A
1744 # "version.texi" file is actually any file whose name matches
1745 # "vers*.texi".
1746 sub scan_texinfo_file
1747 {
1748     local ($filename) = @_;
1749
1750     if (! open (TEXI, $filename))
1751     {
1752         &am_error ("couldn't open \`$filename': $!");
1753         return '';
1754     }
1755     print "automake: reading $filename\n" if $verbose;
1756
1757     local ($vfile, $outfile);
1758     while (<TEXI>)
1759     {
1760         if (/^\@setfilename +(\S+)/)
1761         {
1762             $outfile = $1;
1763             last if ($vfile);
1764         }
1765
1766         if (/^\@include\s+(vers[^.]*\.texi)\s*$/)
1767         {
1768             # Found version.texi include.
1769             $vfile = $1;
1770             last if $outfile;
1771         }
1772     }
1773
1774     close (TEXI);
1775     return ($outfile, $vfile);
1776 }
1777
1778 # Handle all Texinfo source.
1779 sub handle_texinfo
1780 {
1781     &am_line_error ('TEXINFOS',
1782                     "\`TEXINFOS' is an anachronism; use \`info_TEXINFOS'")
1783         if &variable_defined ('TEXINFOS');
1784     return if (! &variable_defined ('info_TEXINFOS')
1785                && ! &variable_defined ('html_TEXINFOS'));
1786
1787     local (@texis) = &variable_value_as_list ('info_TEXINFOS');
1788
1789     local (@info_deps_list, @dvis_list, @texi_deps);
1790     local ($infobase, $info_cursor);
1791     local (%versions);
1792     local ($done) = 0;
1793     local ($vti);
1794     local ($tc_cursor, @texi_cleans);
1795     local ($canonical);
1796
1797     foreach $info_cursor (@texis)
1798     {
1799         ($infobase = $info_cursor) =~ s/\.texi(nfo)?$//;
1800
1801         # If 'version.texi' is referenced by input file, then include
1802         # automatic versioning capability.
1803         local ($out_file, $vtexi) = &scan_texinfo_file ($relative_dir
1804                                                         . "/" . $info_cursor);
1805
1806         if ($out_file eq '')
1807         {
1808             &am_error ("\`$info_cursor' missing \@setfilename");
1809             next;
1810         }
1811
1812         if ($out_file =~ /\.(.+)$/ && $1 ne 'info')
1813         {
1814             # FIXME should report line number in input file.
1815             &am_error ("output of \`$info_cursor', \`$out_file', has unrecognized extension");
1816             next;
1817         }
1818
1819         if ($vtexi)
1820         {
1821             &am_error ("\`$vtexi', included in \`$info_cursor', also included in \`$versions{$vtexi}'")
1822                 if (defined $versions{$vtexi});
1823             $versions{$vtexi} = $info_cursor;
1824
1825             # We number the stamp-vti files.  This is doable since the
1826             # actual names don't matter much.  We only number starting
1827             # with the second one, so that the common case looks nice.
1828             $vti = 'vti' . ($done ? $done : '');
1829             &push_dist_common ($vtexi, 'stamp-' . $vti);
1830             push (@clean, $vti);
1831
1832             # Only require once.
1833             &require_conf_file_with_line ('info_TEXINFOS', $FOREIGN,
1834                                           'mdate-sh')
1835                 if ! $done;
1836             ++$done;
1837
1838             local ($conf_pat);
1839             ($conf_pat = $config_aux_dir) =~ s/(\W)/\\$1/g;
1840             $output_rules .=
1841                 &file_contents_with_transform
1842                     ('s/\@TEXI\@/' . $info_cursor . '/g; '
1843                      . 's/\@VTI\@/' . $vti . '/g; '
1844                      . 's/\@VTEXI\@/' . $vtexi . '/g;'
1845                      . 's,\@MDDIR\@,' . $conf_pat . ',g;',
1846                      'texi-vers');
1847
1848             &push_phony_cleaners ($vti);
1849         }
1850
1851         # If user specified file_TEXINFOS, then use that as explicit
1852         # dependency list.
1853         @texi_deps = ();
1854         push (@texi_deps, $info_cursor);
1855         push (@texi_deps, $vtexi) if $vtexi;
1856
1857         # Canonicalize name first.
1858         ($canonical = $infobase) =~ tr/A-Za-z0-9_/_/c;
1859         if (&variable_defined ($canonical . "_TEXINFOS"))
1860         {
1861             push (@texi_deps, '$(' . $canonical . '_TEXINFOS)');
1862             &push_dist_common ('$(' . $canonical . '_TEXINFOS)');
1863         }
1864
1865         $output_rules .= ("\n" . $out_file . ": "
1866                           . join (' ', @texi_deps)
1867                           . "\n" . $infobase . ".dvi: "
1868                           . join (' ', @texi_deps)
1869                           . "\n\n");
1870
1871         push (@info_deps_list, $out_file);
1872         push (@dvis_list, $infobase . '.dvi');
1873
1874         # Generate list of things to clean for this target.  We do
1875         # this explicitly because otherwise too many things could be
1876         # removed.  In particular the ".log" extension might
1877         # reasonably be used in other contexts by the user.
1878         foreach $tc_cursor ('aux', 'cp', 'cps', 'dvi', 'fn', 'fns',
1879                             'ky', 'log', 'pg', 'toc', 'tp', 'tps',
1880                             'vr', 'vrs', 'op', 'tr', 'cv')
1881         {
1882             push (@texi_cleans, $infobase . '.' . $tc_cursor);
1883         }
1884     }
1885
1886     # Find these programs wherever they may lie.  Yes, this has
1887     # intimate knowledge of the structure of the texinfo distribution.
1888     &define_program_variable ('MAKEINFO', 'build', 'texinfo/makeinfo',
1889                               '@MAKEINFO@');
1890     &define_program_variable ('TEXI2DVI', 'src', 'texinfo/util',
1891                               'texi2dvi');
1892
1893     # Set transform for including texinfos.am.  First, handle --cygnus
1894     # stuff.
1895     local ($xform);
1896     if ($cygnus_mode)
1897     {
1898         $xform = 's/^NOTCYGNUS.*$//; s/^CYGNUS//;';
1899     }
1900     else
1901     {
1902         $xform = 's/^CYGNUS.*$//; s/^NOTCYGNUS//;';
1903     }
1904
1905     # Handle location of texinfo.tex.
1906     if ($cygnus_mode)
1907     {
1908         &define_variable ('TEXINFO_TEX',
1909                           '$(top_srcdir)/../texinfo/texinfo.tex');
1910     }
1911     elsif (! &variable_defined ('TEXINFO_TEX'))
1912     {
1913         &define_variable ('TEXINFO_TEX', '$(srcdir)/texinfo.tex');
1914     }
1915     local ($xxform) = &dirname ($contents{'TEXINFO_TEX'});
1916     $xxform =~ s/(\W)/\\$1/g;
1917     $xform .= ' s/\@TEXINFODIR\@/' . $xxform . '/g;';
1918
1919     $output_rules .= &file_contents_with_transform ($xform, 'texinfos');
1920     push (@phony, 'install-info-am', 'uninstall-info');
1921     push (@dist_targets, 'dist-info');
1922
1923     # How to clean.
1924     $output_rules .= "\nmostlyclean-info:\n";
1925     &pretty_print_rule ("\trm -f", "\t  ", @texi_cleans);
1926     $output_rules .= ("\nclean-info:\n\ndistclean-info:\n\n"
1927                       . "maintainer-clean-info:\n\t"
1928                       . 'for i in $(INFO_DEPS); do rm -f `eval echo $$i*`; done'
1929                       . "\n");
1930     &push_phony_cleaners ('info');
1931
1932     push (@suffixes, '.texi', '.texinfo', '.info', '.dvi', '.ps');
1933
1934     if (! defined $options{'no-installinfo'})
1935     {
1936         push (@uninstall, 'uninstall-info');
1937         push (@installdirs, '$(infodir)');
1938         unshift (@install_data, 'install-info-am');
1939
1940         # Make sure documentation is made and installed first.  Use
1941         # $(INFO_DEPS), not 'info', because otherwise recursive makes
1942         # get run twice during "make all".
1943         unshift (@all, '$(INFO_DEPS)');
1944     }
1945     push (@clean, 'info');
1946     push (@info, '$(INFO_DEPS)');
1947     push (@dvi, '$(DVIS)');
1948
1949     &define_variable ("INFO_DEPS", join (' ', @info_deps_list));
1950     &define_variable ("DVIS", join (' ', @dvis_list));
1951     # This next isn't strictly needed now -- the places that look here
1952     # could easily be changed to look in info_TEXINFOS.  But this is
1953     # probably better, in case noinst_TEXINFOS is ever supported.
1954     &define_variable ("TEXINFOS", $contents{'info_TEXINFOS'});
1955
1956     # Do some error checking.  Note that this file is not required
1957     # when in Cygnus mode; instead we defined TEXINFO_TEX explicitly
1958     # up above.
1959     &require_file_with_line ('info_TEXINFOS', $FOREIGN, 'texinfo.tex')
1960         unless (defined $options{'no-texinfo.tex'}
1961                 || &variable_defined ('TEXINFO_TEX'));
1962 }
1963
1964 # Handle any man pages.
1965 sub handle_man_pages
1966 {
1967     &am_line_error ('MANS', "\`MANS' is an anachronism; use \`man_MANS'")
1968         if &variable_defined ('MANS');
1969     return if ! &variable_defined ('man_MANS');
1970
1971     # We generate the manpage install code by hand to avoid the use of
1972     # basename in the generated Makefile.
1973     local (@mans) = &variable_value_as_list ('man_MANS');
1974     local (%sections, %inames, %mbases, %secmap, %fullsecmap);
1975     local ($i) = 1;
1976     foreach (@mans)
1977     {
1978         # FIXME: statement without effect:
1979         /^(.*)\.([0-9])([a-z]*)$/;
1980         $sections{$2} = 1;
1981         $inames{$i} = $_;
1982         $mbases{$i} = $1;
1983         $secmap{$i} = $2;
1984         $fullsecmap{$i} = $2 . $3;
1985         $i++;
1986     }
1987
1988     # We don't really need this, but we use it in case we ever want to
1989     # support noinst_MANS.
1990     &define_variable ("MANS", $contents{'man_MANS'});
1991
1992     # Generate list of install dirs.
1993     $output_rules .= "install-man: \$(MANS)\n";
1994     $output_rules .= "\t\$(NORMAL_INSTALL)\n";
1995     # Sort keys so that output is deterministic.
1996     foreach (sort keys %sections)
1997     {
1998         push (@installdirs, '$(mandir)/man' . $_)
1999             unless defined $options{'no-installman'};
2000         $output_rules .= ("\t" . '$(mkinstalldirs) $(mandir)/man'
2001                           . $_ . "\n");
2002     }
2003     push (@phony, 'install-man');
2004
2005     # Generate install target.
2006     local ($key);
2007     foreach $key (sort keys %inames)
2008     {
2009         $_ = $install_man_format;
2010         s/\@SECTION\@/$secmap{$key}/g;
2011         s/\@MAN\@/$inames{$key}/g;
2012         s/\@FULLSECT\@/$fullsecmap{$key}/g;
2013         s/\@MANBASE\@/$mbases{$key}/g;
2014         $output_rules .= $_;
2015     }
2016     $output_rules .= "\n";
2017
2018     $output_rules .= "uninstall-man:\n\t\$(NORMAL_UNINSTALL)\n";
2019     foreach $key (sort keys %inames)
2020     {
2021         $_ = $uninstall_man_format;
2022         s/\@SECTION\@/$secmap{$key}/g;
2023         s/\@MAN\@/$inames{$key}/g;
2024         s/\@FULLSECT\@/$fullsecmap{$key}/g;
2025         s/\@MANBASE\@/$mbases{$key}/g;
2026         $output_rules .= $_;
2027     }
2028     $output_rules .= "\n";
2029     push (@phony, 'uninstall-man');
2030
2031     $output_vars .= &file_contents ('mans-vars');
2032
2033     if (! defined $options{'no-installman'})
2034     {
2035         push (@install_data, 'install-man');
2036         push (@uninstall, 'uninstall-man');
2037         push (@all, '$(MANS)');
2038     }
2039 }
2040
2041 # Handle DATA variables.
2042 sub handle_data
2043 {
2044     &am_install_var ('data', 'DATA', 'data', 'sysconf',
2045                      'sharedstate', 'localstate', 'pkgdata',
2046                      'noinst', 'check');
2047 }
2048
2049 # Handle TAGS.
2050 sub handle_tags
2051 {
2052     push (@phony, 'tags');
2053     local (@tag_deps) = ();
2054     if (&variable_defined ('SUBDIRS'))
2055     {
2056         $output_rules .= ("tags-recursive:\n"
2057                           . "\tlist=\"\$(SUBDIRS)\"; for subdir in \$\$list; do \\\n"
2058                           # Never fail here if a subdir fails; it
2059                           # isn't important.
2060                           . "\t  (cd \$\$subdir && \$(MAKE) tags); \\\n"
2061                           . "\tdone\n");
2062         push (@tag_deps, 'tags-recursive');
2063         push (@phony, 'tags-recursive');
2064     }
2065
2066     if ($dir_holds_sources
2067         || $dir_holds_headers
2068         || &variable_defined ('ETAGS_ARGS')
2069         || @tag_deps)
2070     {
2071         local ($xform) = '';
2072         if ($config_header && $relative_dir eq &dirname ($config_header))
2073         {
2074             # The config header is in this directory.  So require it.
2075             ($xform = &basename ($config_header)) =~ s/(\W)/\\$1/g;
2076         }
2077         $xform = ('s/\@CONFIG\@/' . $xform . '/;'
2078                   . 's/\@DIRS\@/' . join (' ', @tag_deps) . '/;');
2079
2080         $output_rules .= &file_contents_with_transform ($xform, 'tags');
2081         $output_rules .= &file_contents ('tags-clean');
2082         push (@clean, 'tags');
2083         &push_phony_cleaners ('tags');
2084         &examine_variable ('TAGS_DEPENDENCIES');
2085     }
2086     elsif (&variable_defined ('TAGS_DEPENDENCIES'))
2087     {
2088         &am_line_error ('TAGS_DEPENDENCIES',
2089                         "doesn't make sense to define \`TAGS_DEPENDENCIES' without sources or \`ETAGS_ARGS'");
2090     }
2091     else
2092     {
2093         # Every Makefile must define some sort of TAGS rule.
2094         # Otherwise, it would be possible for a top-level "make TAGS"
2095         # to fail because some subdirectory failed.
2096         $output_rules .= "tags: TAGS\nTAGS:\n\n";
2097     }
2098 }
2099
2100 # Worker for handle_dist.
2101 sub handle_dist_worker
2102 {
2103     $output_rules .= 'distdir: $(DISTFILES)' . "\n";
2104
2105     # Initialization; only at top level.
2106     if ($relative_dir eq '.')
2107     {
2108         if ($strictness >= $GNITS)
2109         {
2110             # For Gnits users, this is pretty handy.  Look at 15 lines
2111             # in case some explanatory text is desirable.
2112             $output_rules .= '  @if sed 15q $(srcdir)/NEWS | fgrep -e "$(VERSION)" > /dev/null; then :; else \\
2113           echo "NEWS not updated; not releasing" 1>&2; \\
2114           exit 1; \\
2115         fi
2116 ';
2117         }
2118
2119
2120         $output_rules .=
2121             # Create dist directory.
2122             '   rm -rf $(distdir)
2123         mkdir $(distdir)
2124         -chmod 755 $(distdir)
2125 ';
2126
2127         # Only run automake in `dist' target if --include-deps not
2128         # specified.  That way the recipient of a distribution can run
2129         # "make dist" and not need Automake.
2130         if ($cmdline_use_dependencies)
2131         {
2132             $output_rules .=
2133                 (
2134                  # We need an absolute path for --output-dir.  Thus the
2135                  # weirdness.
2136                  '      here=`pwd`; distdir=`cd $(distdir) && pwd` \\
2137           && cd $(srcdir) \\
2138           && $(AUTOMAKE) --include-deps --build-dir=$$here --srcdir-name=$(srcdir) --output-dir=$$distdir '
2139                  # Set strictness of output.
2140                  . ($cygnus_mode ? '--cygnus' : ('--' . $strictness_name))
2141                  . "\n"
2142                  );
2143         }
2144     }
2145
2146     # Scan EXTRA_DIST to see if we need to distribute anything from a
2147     # subdir.  If so, add it to the list.  I didn't want to do this
2148     # originally, but there were so many requests that I finally
2149     # relented.
2150     local (@dist_dirs);
2151     if (&variable_defined ('EXTRA_DIST'))
2152     {
2153         foreach (&variable_value_as_list ('EXTRA_DIST'))
2154         {
2155             next if /^\@.*\@$/;
2156             next unless s,/+[^/]+$,,;
2157             push (@dist_dirs, $_)
2158                 unless $_ eq '.';
2159         }
2160     }
2161     if (@dist_dirs)
2162     {
2163         # Prepend $(distdir) to each directory given.  Doing it via a
2164         # hash lets us ensure that each directory is used only once.
2165         local (%dhash);
2166         grep ($dhash{'$(distdir)/' . $_} = 1, @dist_dirs);
2167         $output_rules .= "\t";
2168         &pretty_print_rule ('$(mkinstalldirs)', "\t   ", sort keys %dhash);
2169     }
2170
2171     # In loop, test for file existence because sometimes a file gets
2172     # included in DISTFILES twice.  For example this happens when a
2173     # single source file is used in building more than one program.
2174     # Also, there are situations in which "ln" can fail.  For instance
2175     # a file to distribute could actually be a cross-filesystem
2176     # symlink -- this can easily happen if "gettextize" was run on the
2177     # distribution.
2178     $output_rules .= "\t\@for file in \$(DISTFILES); do \\\n";
2179     if ($cygnus_mode)
2180     {
2181         $output_rules .= "\t  if test -f \$\$file; then d=.; else d=\$(srcdir); fi; \\\n";
2182     }
2183     else
2184     {
2185         $output_rules .= "\t  d=\$(srcdir); \\\n";
2186     }
2187     $output_rules .= '    test -f $(distdir)/$$file \\
2188           || ln $$d/$$file $(distdir)/$$file 2> /dev/null \\
2189           || cp -p $$d/$$file $(distdir)/$$file; \\
2190         done
2191 ';
2192
2193     # If we have SUBDIRS, create all dist subdirectories and do
2194     # recursive build.
2195     if (&variable_defined ('SUBDIRS'))
2196     {
2197         # Test for directory existence here because previous automake
2198         # invocation might have created some directories.  Note that
2199         # we explicitly set distdir for the subdir make; that lets us
2200         # mix-n-match many automake-using packages into one large
2201         # package, and have "dist" at the top level do the right
2202         # thing.
2203         $output_rules .= '      for subdir in $(SUBDIRS); do            \\
2204           test -d $(distdir)/$$subdir           \\
2205           || mkdir $(distdir)/$$subdir          \\
2206           || exit 1;                            \\
2207           chmod 755 $(distdir)/$$subdir;        \\
2208           (cd $$subdir && $(MAKE) distdir=../$(distdir)/$$subdir distdir) \\
2209             || exit 1; \\
2210         done
2211 ';
2212     }
2213
2214     # If the target `dist-hook' exists, make sure it is run.  This
2215     # allows users to do random weird things to the distribution
2216     # before it is packaged up.
2217     push (@dist_targets, 'dist-hook') if defined $contents{'dist-hook'};
2218
2219     local ($targ);
2220     foreach $targ (@dist_targets)
2221     {
2222         # We must explicitly set distdir for these sub-makes.
2223         $output_rules .= "\t\$(MAKE) distdir=\"\$(distdir)\" $targ\n";
2224     }
2225
2226     push (@phony, 'distdir');
2227 }
2228
2229 # Handle 'dist' target.
2230 sub handle_dist
2231 {
2232     # Set up maint_charset.
2233     $local_maint_charset = $contents{'MAINT_CHARSET'}
2234         if &variable_defined ('MAINT_CHARSET');
2235     $maint_charset = $local_maint_charset
2236         if $relative_dir eq '.';
2237
2238     if (&variable_defined ('DIST_CHARSET'))
2239     {
2240         &am_line_error ('DIST_CHARSET',
2241                         "DIST_CHARSET defined but no MAINT_CHARSET defined")
2242             if ! $local_maint_charset;
2243         if ($relative_dir eq '.')
2244         {
2245             $dist_charset = $contents{'DIST_CHARSET'}
2246         }
2247         else
2248         {
2249             &am_line_error ('DIST_CHARSET',
2250                             "DIST_CHARSET can only be defined at top level");
2251         }
2252     }
2253
2254     # Look for common files that should be included in distribution.
2255     local ($cfile);
2256     foreach $cfile (@common_files)
2257     {
2258         if (-f ($relative_dir . "/" . $cfile))
2259         {
2260             &push_dist_common ($cfile);
2261         }
2262     }
2263
2264     # Keys of %dist_common are names of files to distributed.  We put
2265     # README first because it then becomes easier to make a
2266     # Usenet-compliant shar file (in these, README must be first).
2267     # FIXME: do more ordering of files here.
2268     local (@coms);
2269     if (defined $dist_common{'README'})
2270     {
2271         push (@coms, 'README');
2272         delete $dist_common{'README'};
2273     }
2274     push (@coms, sort keys %dist_common);
2275
2276     &define_pretty_variable ("DIST_COMMON", @coms);
2277     $output_vars .= "\n";
2278
2279     # Some boilerplate.
2280     $output_vars .= &file_contents ('dist-vars') . "\n";
2281     &define_variable ('TAR', $TAR);
2282     &define_variable ('GZIP', '--best');
2283
2284     # Put these things in rules section so it is easier for whoever
2285     # reads Makefile.in.
2286     if (! &variable_defined ('distdir'))
2287     {
2288         if ($relative_dir eq '.')
2289         {
2290             $output_rules .= "\n" . 'distdir = $(PACKAGE)-$(VERSION)' . "\n";
2291         }
2292         else
2293         {
2294             $output_rules .= ("\n"
2295                               . 'distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)'
2296                               . "\n");
2297         }
2298     }
2299     if ($relative_dir ne '.')
2300     {
2301         $output_rules .= "\nsubdir = " . $relative_dir . "\n";
2302     }
2303
2304     # Generate 'dist' target, and maybe dist-shar / dist-zip / dist-tarZ.
2305     if ($relative_dir eq '.')
2306     {
2307         # Rule to check whether a distribution is viable.
2308         $output_rules .= ('# This target untars the dist file and tries a VPATH configuration.  Then
2309 # it guarantees that the distribution is self-contained by making another
2310 # tarfile.
2311 distcheck: dist
2312         rm -rf $(distdir)
2313         GZIP=$(GZIP) $(TAR) zxf $(distdir).tar.gz
2314         mkdir $(distdir)/=build
2315         mkdir $(distdir)/=inst
2316         dc_install_base=`cd $(distdir)/=inst && pwd`; \\'
2317                           . (defined $contents{'distcheck-hook'}
2318                              ? "\t\$(MAKE) distcheck-hook"
2319                              : '')
2320                           . '
2321         cd $(distdir)/=build \\
2322           && ../configure '
2323
2324                           . ($seen_gettext ? '--with-included-gettext ' : '')
2325                           . '--srcdir=.. --prefix=$$dc_install_base \\
2326           && $(MAKE) \\
2327           && $(MAKE) dvi \\
2328           && $(MAKE) check \\
2329           && $(MAKE) install \\
2330           && $(MAKE) installcheck \\
2331           && $(MAKE) dist
2332         rm -rf $(distdir)
2333         @echo "========================"; \\
2334         echo "$(distdir).tar.gz is ready for distribution"; \\
2335         echo "========================"
2336 ');
2337
2338         local ($dist_all) = ('dist-all: distdir' . "\n"
2339                              . $dist_header);
2340         local ($curs);
2341         foreach $curs ('dist', 'dist-shar', 'dist-zip', 'dist-tarZ')
2342         {
2343             if (defined $options{$curs} || $curs eq 'dist')
2344             {
2345                 $output_rules .= ($curs . ': distdir' . "\n"
2346                                   . $dist_header
2347                                   . $dist{$curs}
2348                                   . $dist_trailer);
2349                 $dist_all .= $dist{$curs};
2350             }
2351         }
2352         $output_rules .= $dist_all . $dist_trailer;
2353     }
2354
2355     # Generate distdir target.
2356     &handle_dist_worker;
2357 }
2358
2359 # Scan a single dependency file and rewrite the dependencies as
2360 # appropriate.  Essentially this means:
2361 # * Clean out absolute dependencies which are not desirable.
2362 # * Rewrite other dependencies to be relative to $(top_srcdir).
2363 sub scan_dependency_file
2364 {
2365     local ($depfile) = @_;
2366
2367     if (! open (DEP_FILE, $depfile))
2368     {
2369         &am_error ("couldn't open \`$depfile': $!");
2370         return;
2371     }
2372     print "automake: reading $depfile\n" if $verbose;
2373
2374     # Sometimes it is necessary to omit some dependencies.
2375     local (%omit) = %omit_dependencies;
2376     if (&variable_defined ('OMIT_DEPENDENCIES'))
2377     {
2378         grep ($omit{$_} = 1, &variable_value_as_list ('OMIT_DEPENDENCIES'));
2379     }
2380
2381     local ($first_line) = 1;
2382     local ($last_line) = 0;
2383     local ($target, @dependencies);
2384     local ($one_dep, $xform);
2385     local ($just_file);
2386
2387     local ($srcdir_rx, $fixup_rx);
2388     # If the top srcdir is absolute, then the current directory is
2389     # just relative_dir.  But if the top srcdir is relative, then we
2390     # need to add some dots first.  The same holds when matching
2391     # srcdir directly.
2392     if ($srcdir_name =~ /^\//)
2393     {
2394         ($fixup_rx = $srcdir_name . '/' . $relative_dir . '/')
2395             =~ s/(\W)/\\$1/g;
2396         ($srcdir_rx = $srcdir_name . '/') =~ s/(\W)/\\$1/g;
2397     }
2398     else
2399     {
2400         ($fixup_rx = ($srcdir_name . '/' . $top_builddir . '/'
2401                       . $relative_dir . '/')) =~ s/(\W)/\\$1/g;
2402         $srcdir_rx = $srcdir_name . '/';
2403         # Don't append a spurious "." to the regex.
2404         $srcdir_rx .= $top_builddir
2405             unless $top_builddir eq '.';
2406         $srcdir_rx =~ s/(\W)/\\$1/g;
2407     }
2408
2409     local ($rewrite_builddir) = (($top_builddir eq '.')
2410                                  ? ''
2411                                  : $top_builddir . '/');
2412
2413     while (<DEP_FILE>)
2414     {
2415         if ($last_line)
2416         {
2417             # If LAST_LINE set then we've already seen what we thought
2418             # was the last line.
2419             goto bad_format;
2420         }
2421         next if (/$WHITE_PATTERN/o);
2422         chop;
2423         if (! s/\\$//)
2424         {
2425             # No trailing "\" means this should be the last line.
2426             $last_line = 1;
2427         }
2428
2429         if ($first_line)
2430         {
2431             if (! /^([^:]+:)(.+)$/)
2432             {
2433               bad_format:
2434                 &am_error ("\`$depfile' has incorrect format");
2435                 close (DEP_FILE);
2436                 return;
2437             }
2438
2439             $target = $1;
2440             $_ = $2;
2441
2442             $first_line = 0;
2443         }
2444
2445         foreach $one_dep (split (' ', $_))
2446         {
2447             if ($one_dep =~ /^$fixup_rx/)
2448             {
2449                 # The dependency points to the current directory in
2450                 # some way.
2451                 ($xform = $one_dep) =~ s/^$fixup_rx//;
2452                 push (@dependencies, $xform);
2453             }
2454             elsif ($one_dep =~ /^$srcdir_rx/)
2455             {
2456                 # The dependency is in some other directory in the package.
2457                 ($xform = $one_dep) =~ s/^$srcdir_rx/$rewrite_builddir/;
2458                 push (@dependencies, $xform);
2459             }
2460             elsif ($one_dep =~ /^\//)
2461             {
2462                 # Absolute path; ignore.
2463             }
2464             else
2465             {
2466                 # Anything else is assumed to be correct.  But first
2467                 # make sure it is not on our list of dependencies to
2468                 # omit.
2469                 ($just_file = $one_dep) =~ s,^.*/,,;
2470                 push (@dependencies, $one_dep)
2471                     if ! defined $omit{$just_file};
2472             }
2473         }
2474     }
2475
2476     &pretty_print_rule ($target, "\t", @dependencies);
2477
2478     close (DEP_FILE);
2479 }
2480
2481 # Handle auto-dependency code.
2482 sub handle_dependencies
2483 {
2484     if ($use_dependencies)
2485     {
2486         # Include GNU-make-specific auto-dep code.
2487         if ($dir_holds_sources)
2488         {
2489             &define_pretty_variable ('DEP_FILES', sort keys %dep_files);
2490             $output_rules .= &file_contents ('depend');
2491             push (@clean, 'depend');
2492             &push_phony_cleaners ('depend');
2493             $output_rules .=
2494                 &file_contents_with_transform ('s/\@EXT\@/.c/g;'
2495                                                . 's/\@MKDEP\@/MKDEP/g;'
2496                                                . 's/^ONLYC//g;',
2497                                                'depend2');
2498             local ($ext);
2499             local ($need_cxx) = 0;
2500             foreach $ext (sort keys %cxx_extensions)
2501             {
2502                 $output_rules .=
2503                     &file_contents_with_transform ('s/\@EXT\@/' . $ext .'/g;'
2504                                                    . 's/\@MKDEP\@/CXXMKDEP/g;'
2505                                                    . 's/^ONLYC.*$//;',
2506                                                    'depend2');
2507                 $need_cxx = 1;
2508             }
2509             if ($need_cxx)
2510             {
2511                 &define_variable ('CXXMKDEP', '$(CXX) -M $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS)');
2512             }
2513         }
2514     }
2515     elsif ($build_directory ne '')
2516     {
2517         # Include any auto-generated deps that are present.  Note that
2518         # $build_directory ends in a "/".
2519         if (-d ($build_directory . $relative_dir . "/.deps")
2520             && -f ($build_directory . $relative_dir . "/.deps/.P"))
2521         {
2522             local ($depfile);
2523
2524             foreach $depfile (&my_glob ($build_directory
2525                                         . $relative_dir . "/.deps/*.P"))
2526             {
2527                 &scan_dependency_file ($depfile);
2528             }
2529
2530             $output_rules .= "\n";
2531         }
2532     }
2533 }
2534
2535 # Handle subdirectories.
2536 sub handle_subdirs
2537 {
2538     return if ! &variable_defined ('SUBDIRS');
2539
2540     # Make sure each directory mentioned in SUBDIRS actually exists.
2541     local ($dir);
2542     foreach $dir (&variable_value_as_list ('SUBDIRS'))
2543     {
2544         # Skip directories substituted by configure.
2545         next if $dir =~ /^\@.*\@$/;
2546
2547         if (! -d $am_relative_dir . '/' . $dir)
2548         {
2549             &am_line_error ('SUBDIRS',
2550                             "required directory $am_relative_dir/$dir does not exist");
2551             next;
2552         }
2553
2554         &am_relative_dir ('SUBDIRS',
2555                           "directory should not contain \`/'")
2556             if $dir =~ /\//;
2557     }
2558
2559     local ($xform) = ('s/\@INSTALLINFO\@/' .
2560                       (defined $options{'no-installinfo'}
2561                        ? 'install-info-recursive'
2562                        : '')
2563                       . '/;');
2564     $output_rules .= &file_contents_with_transform ($xform, 'subdirs');
2565
2566     # Push a bunch of phony targets.
2567     local ($phonies);
2568     foreach $phonies ('-data', '-exec', 'dirs')
2569     {
2570         push (@phony, 'install' . $phonies . '-recursive');
2571         push (@phony, 'uninstall' . $phonies . '-recursive');
2572     }
2573     foreach $phonies ('all', 'check', 'installcheck', 'info', 'dvi')
2574     {
2575         push (@phony, $phonies . '-recursive');
2576     }
2577     &push_phony_cleaners ('recursive');
2578
2579     push (@check_tests, "check-recursive");
2580     push (@installcheck, "installcheck-recursive");
2581     push (@info, "info-recursive");
2582     push (@dvi, "dvi-recursive");
2583
2584     $recursive_install = 1;
2585 }
2586
2587 # Handle aclocal.m4.
2588 sub handle_aclocal_m4
2589 {
2590     local ($regen_aclocal) = 0;
2591     if (-f 'aclocal.m4')
2592     {
2593         &define_variable ("ACLOCAL_M4", '$(top_srcdir)/aclocal.m4');
2594         &push_dist_common ('aclocal.m4');
2595
2596         if (open (ACLOCAL, '< aclocal.m4'))
2597         {
2598             local ($line);
2599             $line = <ACLOCAL>;
2600             close (ACLOCAL);
2601
2602             if ($line =~ 'generated automatically by aclocal')
2603             {
2604                 $regen_aclocal = 1;
2605             }
2606         }
2607     }
2608
2609     local ($acinclude) = 0;
2610     if (-f 'acinclude.m4')
2611     {
2612         $regen_aclocal = 1;
2613         $acinclude = 1;
2614     }
2615
2616     # Note that it might be possible that aclocal.m4 doesn't exist but
2617     # should be auto-generated.  This case probably isn't very
2618     # important.
2619     if ($regen_aclocal)
2620     {
2621         local (@ac_deps) = (
2622                             ($seen_maint_mode ? "\@MAINT\@" : "") ,
2623                             "configure.in",
2624                             ($acinclude ? ' acinclude.m4' : '')
2625                             );
2626
2627         # Scan all -I directories for m4 files.  These are our
2628         # dependencies.
2629         if (&variable_defined ('ACLOCAL_AMFLAGS'))
2630         {
2631             local ($amdir);
2632             foreach $amdir (&variable_value_as_list ('ACLOCAL_AMFLAGS'))
2633             {
2634                 if ($amdir =~ s/^-I//
2635                     && $amdir !~ /^\//
2636                     && -d $amdir)
2637                 {
2638                     push (@ac_deps, &my_glob ($am_dir . '/*.m4'));
2639                 }
2640             }
2641         }
2642
2643         &pretty_print_rule ("\$(srcdir)/aclocal.m4:", "\t\t", @ac_deps);
2644
2645         $output_rules .=  ("\t"
2646                            . 'cd $(srcdir) && $(ACLOCAL)'
2647                            . (&variable_defined ('ACLOCAL_AMFLAGS')
2648                               ? ' $(ACLOCAL_AMFLAGS)' : '')
2649                            . "\n");
2650     }
2651 }
2652
2653 # Rewrite a list of input files into a form suitable to put on a
2654 # dependency list.  The idea is that if an input file has a directory
2655 # part the same as the current directory, then the directory part is
2656 # simply removed.  But if the directory part is different, then
2657 # $(top_srcdir) is prepended.  Among other things, this is used to
2658 # generate the dependency list for the output files generated by
2659 # AC_OUTPUT.  Consider what the dependencies should look like in this
2660 # case:
2661 #   AC_OUTPUT(src/out:src/in1:lib/in2)
2662 sub rewrite_inputs_into_dependencies
2663 {
2664     local (@inputs) = @_;
2665     local ($single, @newinputs);
2666
2667     foreach $single (@inputs)
2668     {
2669         if (&dirname ($single) eq $relative_dir)
2670         {
2671             push (@newinputs, &basename ($single));
2672         }
2673         else
2674         {
2675             push (@newinputs, '$(top_srcdir)/' . $single);
2676         }
2677     }
2678
2679     return @newinputs;
2680 }
2681
2682 # Handle remaking and configure stuff.
2683 # We need the name of the input file, to do proper remaking rules.
2684 sub handle_configure
2685 {
2686     local ($local, $input, @secondary_inputs) = @_;
2687
2688     # If SUBDIRS defined, require AC_PROG_MAKE_SET.
2689     &am_line_error ('SUBDIRS', "AC_PROG_MAKE_SET must be used in configure.in")
2690         if &variable_defined ('SUBDIRS') && ! $seen_make_set;
2691
2692     local ($top_reldir);
2693
2694     local ($input_base) = &basename ($input);
2695     local ($local_base) = &basename ($local);
2696
2697     local ($amfile) = $input_base . '.am';
2698     # We know we can always add '.in' because it really should be an
2699     # error if the .in was missing originally.
2700     local ($infile) = '$(srcdir)/' . $input_base . '.in';
2701     local ($colon_infile);
2702     if ($local ne $input)
2703     {
2704         $colon_infile = ':' . $input . '.in';
2705     }
2706     $colon_infile .= ':' . join (':', @secondary_inputs)
2707         if @secondary_inputs;
2708
2709     local (@rewritten) = &rewrite_inputs_into_dependencies (@secondary_inputs);
2710     # This rule remakes the Makefile.in.  Note use of @MAINT@ forces
2711     # us to abandon pretty-printing.  Sigh.
2712     $output_rules .= ($infile
2713                       # NOTE perl 5.003 (with -w) gives a
2714                       # uninitialized value error on the next line.
2715                       # Don't know why.
2716                       . ': '
2717                       . ($seen_maint_mode ? '@MAINT@ ' : '')
2718                       . $amfile . ' '
2719                       . '$(top_srcdir)/configure.in $(ACLOCAL_M4) '
2720                       . join (' ', @rewritten) . "\n"
2721                       . "\tcd \$(top_srcdir) && \$(AUTOMAKE) "
2722                       . ($cygnus_mode ? '--cygnus' : ('--' . $strictness_name))
2723                       . ' ' . $input . $colon_infile . "\n\n");
2724
2725     # This rule remakes the Makefile.
2726     $output_rules .= ($local_base
2727                       # NOTE: bogus uninit value error on next line;
2728                       # see comment above.
2729                       . ': '
2730                       . $infile . ' '
2731                       . '$(top_builddir)/config.status $(BUILT_SOURCES)'
2732                       . "\n"
2733                       . "\tcd \$(top_builddir) \\\n"
2734                       . "\t  && CONFIG_FILES="
2735                       . (($relative_dir eq '.') ? '$@' : '$(subdir)/$@')
2736                       . $colon_infile
2737                       . ' CONFIG_HEADERS= $(SHELL) ./config.status'
2738                       . "\n\n");
2739
2740     if ($relative_dir ne '.')
2741     {
2742         # In subdirectory.
2743         $top_reldir = '../';
2744     }
2745     else
2746     {
2747         &handle_aclocal_m4;
2748         $output_rules .= &file_contents ('remake');
2749         &examine_variable ('CONFIGURE_DEPENDENCIES');
2750         $top_reldir = '';
2751     }
2752
2753     # If we have a configure header, require it.
2754     if ($config_header && $relative_dir eq &dirname ($config_header))
2755     {
2756         local ($ch_sans_dir) = &basename ($config_header);
2757         local ($cn_sans_dir) = &basename ($config_name);
2758
2759         &require_file_with_conf_line ($config_header_line,
2760                                       $FOREIGN, $ch_sans_dir);
2761
2762         # Header defined and in this directory.
2763         local (@files);
2764         if (-f $relative_dir . '/acconfig.h')
2765         {
2766             push (@files, 'acconfig.h');
2767         }
2768         if (-f $config_name . '.top')
2769         {
2770             push (@files, "${cn_sans_dir}.top");
2771         }
2772         if (-f $config_name . '.bot')
2773         {
2774             push (@files, "${cn_sans_dir}.bot");
2775         }
2776
2777         &push_dist_common (@files);
2778         $output_rules .= &file_contents_with_transform ('s/\@FILES\@/'
2779                                                         . join (' ', @files)
2780                                                         . '/;',
2781                                                         'remake-hdr');
2782
2783         &touch ($relative_dir . '/stamp-h.in');
2784         &require_file_with_conf_line ($config_header_line, $FOREIGN,
2785                                       'stamp-h.in');
2786
2787         push (@clean, 'hdr');
2788         &push_phony_cleaners ('hdr');
2789         &define_variable ("CONFIG_HEADER_IN", "${ch_sans_dir}");
2790         &define_variable ('CONFIG_HEADER_FULL', $config_name);
2791     }
2792
2793     # Set location of mkinstalldirs.
2794     if ($config_aux_dir ne '.' && $config_aux_dir ne '')
2795     {
2796         &define_variable ('mkinstalldirs', ('$(SHELL) ' . $config_aux_dir
2797                                             . '/mkinstalldirs'));
2798     }
2799     else
2800     {
2801         &define_variable ('mkinstalldirs',
2802                           '$(SHELL) $(top_srcdir)/mkinstalldirs');
2803     }
2804
2805     &am_line_error ('CONFIG_HEADER',
2806                     "\`CONFIG_HEADER' is an anachronism; now determined from \`configure.in'")
2807         if &variable_defined ('CONFIG_HEADER');
2808
2809     if ($config_name)
2810     {
2811         # Generate CONFIG_HEADER define.
2812         if ($relative_dir eq &dirname ($config_name))
2813         {
2814             &define_variable ("CONFIG_HEADER", &basename ($config_name));
2815         }
2816         else
2817         {
2818             &define_variable ("CONFIG_HEADER",
2819                               "${top_builddir}/${config_name}");
2820         }
2821     }
2822
2823     # Now look for other files in this directory which must be remade
2824     # by config.status, and generate rules for them.
2825     local (@actual_other_files) = ();
2826     local ($file, $local);
2827     local (@inputs, @rewritten_inputs, $single);
2828     foreach $file (@other_input_files)
2829     {
2830         if ($file =~ /^(.*):(.*)$/)
2831         {
2832             # This is the ":" syntax of AC_OUTPUT.
2833             $file = $1;
2834             $local = &basename ($file);
2835             @inputs = split (':', $2);
2836             @rewritten_inputs = &rewrite_inputs_into_dependencies (@inputs);
2837         }
2838         else
2839         {
2840             # Normal usage.
2841             $local = &basename ($file);
2842             @inputs = ($local . '.in');
2843             @rewritten_inputs =
2844                 &rewrite_inputs_into_dependencies ($file . '.in');
2845         }
2846
2847         # Skip files not in this directory.
2848         next unless &dirname ($file) eq $relative_dir;
2849
2850         # Skip the config header.
2851         next if $local eq $top_builddir . '/' . $config_name;
2852
2853         # Skip any Makefile.
2854         next if $local eq 'Makefile';
2855
2856         $output_rules .= ($local . ': '
2857                           . '$(top_builddir)/config.status '
2858                           . join (' ', @rewritten_inputs) . "\n"
2859                           . "\t"
2860                           . 'cd $(top_builddir) && CONFIG_FILES='
2861                           . ($relative_dir eq '.' ? '' : '$(subdir)/')
2862                           . '$@' . (length (@rewritten_inputs) > 1
2863                                     ? (':' . join (':', @rewritten_inputs))
2864                                     : '')
2865                           . ' CONFIG_HEADERS= ./config.status'
2866                           . "\n");
2867         push (@actual_other_files, $local);
2868
2869         # Require all input files.
2870         &require_file_with_conf_line ($ac_output_line, $FOREIGN,
2871                                       @inputs);
2872     }
2873
2874     # These files get removed by "make clean".
2875     &define_pretty_variable ('CONFIG_CLEAN_FILES', @actual_other_files);
2876 }
2877
2878 # Handle C headers.
2879 sub handle_headers
2880 {
2881     $dir_holds_headers = &am_install_var ('header', 'HEADERS', 'include',
2882                                           'oldinclude', 'pkginclude',
2883                                           'noinst', 'check');
2884 }
2885
2886 sub handle_gettext
2887 {
2888     return if ! $seen_gettext || $relative_dir ne '.';
2889
2890     if (! &variable_defined ('SUBDIRS'))
2891     {
2892         &am_conf_error
2893             ("AM_GNU_GETTEXT in configure.in but SUBDIRS not defined");
2894         return;
2895     }
2896
2897     &require_file_with_conf_line ($ac_gettext_line, $FOREIGN, 'ABOUT-NLS')
2898         if $seen_gettext;
2899
2900     if (&variable_defined ('SUBDIRS'))
2901     {
2902         &am_line_error
2903             ('SUBDIRS',
2904              "AM_GNU_GETTEXT in configure.in but \`po' not in SUBDIRS")
2905                 if $contents{'SUBDIRS'} !~ /\bpo\b/;
2906         &am_line_error
2907             ('SUBDIRS',
2908              "AM_GNU_GETTEXT in configure.in but \`intl' not in SUBDIRS")
2909                 if $contents{'SUBDIRS'} !~ /\bintl\b/;
2910     }
2911
2912     # Ensure that each language in ALL_LINGUAS has a .po file, and
2913     # each po file is mentioned in ALL_LINGUAS.
2914     if ($seen_linguas)
2915     {
2916         local (%linguas) = ();
2917         grep ($linguas{$_} = 1, split (' ', $all_linguas));
2918
2919         foreach (<po/*.po>)
2920         {
2921             s/^po\///;
2922             s/\.po$//;
2923
2924             &am_line_error ($all_linguas_line,
2925                             ("po/$_.po exists but \`$_' not in \`ALL_LINGUAS'"))
2926                 if ! $linguas{$_};
2927         }
2928
2929         foreach (keys %linguas)
2930         {
2931             &am_line_error ($all_linguas_line,
2932                             "$_ in \`ALL_LINGUAS' but po/$_.po does not exist")
2933                 if ! -f "po/$_.po";
2934         }
2935     }
2936     else
2937     {
2938         &am_error ("AM_GNU_GETTEXT in configure.in but \`ALL_LINGUAS' not defined");
2939     }
2940 }
2941
2942 # Handle footer elements.
2943 sub handle_footer
2944 {
2945     if ($contents{'SOURCES'})
2946     {
2947         # NOTE don't use define_pretty_variable here, because
2948         # $contents{...} is already defined.
2949         $output_vars .= 'SOURCES = ' . $contents{'SOURCES'} . "\n";
2950     }
2951     if ($contents{'OBJECTS'})
2952     {
2953         # NOTE don't use define_pretty_variable here, because
2954         # $contents{...} is already defined.
2955         $output_vars .= 'OBJECTS = ' . $contents{'OBJECTS'} . "\n";
2956     }
2957     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
2958     {
2959         $output_vars .= "\n";
2960     }
2961
2962     if (&variable_defined ('SUFFIXES'))
2963     {
2964         # Push actual suffixes, and not $(SUFFIXES).  Some versions of
2965         # make do not like variable substitutions on the .SUFFIXES
2966         # line.
2967         push (@suffixes, &variable_value_as_list ('SUFFIXES'));
2968     }
2969     if (&target_defined ('.SUFFIXES'))
2970     {
2971         &am_line_error ('.SUFFIXES',
2972                         "use variable \`SUFFIXES', not target \`.SUFFIXES'");
2973     }
2974
2975     # Note: AIX 4.1 /bin/make will fail if any suffix rule appears
2976     # before .SUFFIXES.  So we make sure that .SUFFIXES appears before
2977     # anything else, by sticking it right after the default: target.
2978     $output_header .= ".SUFFIXES:\n";
2979     if (@suffixes)
2980     {
2981         $output_header .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
2982     }
2983     $output_trailer .= &file_contents ('footer');
2984 }
2985
2986 # Deal with installdirs target.
2987 sub handle_installdirs
2988 {
2989     # GNU Makefile standards recommend this.
2990     $output_rules .= ("installdirs:"
2991                       . ($recursive_install
2992                          ? " installdirs-recursive\n"
2993                          : "\n"));
2994     push (@phony, 'installdirs');
2995     if (@installdirs)
2996     {
2997         &pretty_print_rule ("\t" . '$(mkinstalldirs) ', "\t\t",
2998                             @installdirs);
2999     }
3000     $output_rules .= "\n";
3001 }
3002
3003 # There are several targets which need to be merged.  This is because
3004 # their complete definition is compiled from many parts.  Note that we
3005 # avoid double colon rules, otherwise we'd use them instead.
3006 sub handle_merge_targets
3007 {
3008     # There are a few install-related variables that you should not define.
3009     local ($var);
3010     foreach $var ('PRE_INSTALL', 'POST_INSTALL', 'NORMAL_INSTALL')
3011     {
3012         if (&variable_defined ($var))
3013         {
3014             &am_line_error ($var, "\`$var' should not be defined");
3015         }
3016     }
3017
3018     push (@all, 'Makefile');
3019     push (@all, &basename ($config_name))
3020         if $config_name && &dirname ($config_name) eq $relative_dir;
3021
3022     &do_one_merge_target ('info', @info);
3023     &do_one_merge_target ('dvi', @dvi);
3024     &do_check_merge_target;
3025     &do_one_merge_target ('installcheck', @installcheck);
3026
3027     if (defined $options{'no-installinfo'})
3028     {
3029         # FIXME: this is kind of a hack; should find another way to
3030         # know that this is required.
3031         local (@dirs);
3032         if (grep ($_ eq 'install-info-am', @phony))
3033         {
3034             push (@dirs, 'install-info-am');
3035         }
3036         if (&variable_defined ('SUBDIRS'))
3037         {
3038             push (@dirs, 'install-info-recursive');
3039         }
3040         &do_one_merge_target ('install-info', @dirs);
3041     }
3042
3043     # Handle the various install targets specially.  We do this so
3044     # that (eg) "make install-exec" will run "install-exec-recursive"
3045     # if required, but "make install" won't run it twice.  Step one is
3046     # to see if the user specified local versions of any of the
3047     # targets we handle.  "all" is treated as one of these since
3048     # "install" can run it.
3049     push (@install_exec, 'install-exec-local')
3050         if defined $contents{'install-exec-local'};
3051     push (@install_data, 'install-data-local')
3052         if defined $contents{'install-data-local'};
3053     push (@uninstall, 'uninstall-local')
3054         if defined $contents{'uninstall-local'};
3055     local ($utarg);
3056     foreach $utarg ('uninstall-data-local', 'uninstall-data-hook',
3057                     'uninstall-exec-local', 'uninstall-exec-hook')
3058     {
3059         if (defined $contents{$utarg})
3060         {
3061             local ($x);
3062             ($x = $utarg) =~ s/(data|exec)-//;
3063             &am_line_error ($utarg, "use \`$x', not \`$utarg'");
3064         }
3065     }
3066     push (@all, 'all-local')
3067         if defined $contents{'all-local'};
3068
3069     if (defined $contents{'install-local'})
3070     {
3071         &am_line_error ('install-local',
3072                         "use \`install-data' or \`install-exec', not \`install'");
3073     }
3074
3075     # Step two: if we are doing recursive makes, write out the
3076     # appropriate rules.
3077     local (@install);
3078     if ($recursive_install)
3079     {
3080         push (@install, 'install-recursive');
3081
3082         if (@all)
3083         {
3084             local (@hackall) = ();
3085             if ($config_name && &dirname ($config_name) eq $relative_dir)
3086             {
3087
3088                 # This is kind of a hack, but I couldn't see a better
3089                 # way to handle it.  In this particular case, we need
3090                 # to make sure config.h is built before we recurse.
3091                 # We can't do this by changing the order of
3092                 # dependencies to the "all" because that breaks when
3093                 # using parallel makes.  Instead we handle things
3094                 # explicitly.
3095                 $output_rules .= ('all-recursive-am: $(CONFIG_HEADER)'
3096                                   . "\n\t" . '$(MAKE) all-recursive'
3097                                   . "\n\n");
3098                 push (@hackall, 'all-recursive-am');
3099                 push (@phony, 'all-recursive-am');
3100             }
3101             else
3102             {
3103                 push (@hackall, 'all-recursive');
3104             }
3105
3106             $output_rules .= ('all-am: '
3107                               . join (' ', @all)
3108                               . "\n\n");
3109             @all = @hackall;
3110             push (@all, 'all-am');
3111             push (@phony, 'all-am');
3112         }
3113         else
3114         {
3115             @all = ('all-recursive');
3116
3117             # Must always generate `all-am' target, so it can be
3118             # referred to elsewhere.
3119             $output_rules .= "all-am:\n";
3120         }
3121         if (@install_exec)
3122         {
3123             $output_rules .= ('install-exec-am: '
3124                               . join (' ', @install_exec)
3125                               . "\n\n");
3126             @install_exec = ('install-exec-recursive', 'install-exec-am');
3127             push (@install, 'install-exec-am');
3128             push (@phony, 'install-exec-am');
3129         }
3130         else
3131         {
3132             @install_exec = ('install-exec-recursive');
3133         }
3134         if (@install_data)
3135         {
3136             $output_rules .= ('install-data-am: '
3137                               . join (' ', @install_data)
3138                               . "\n\n");
3139             @install_data = ('install-data-recursive', 'install-data-am');
3140             push (@install, 'install-data-am');
3141             push (@phony, 'install-data-am');
3142         }
3143         else
3144         {
3145             @install_data = ('install-data-recursive');
3146         }
3147         if (@uninstall)
3148         {
3149             $output_rules .= ('uninstall-am: '
3150                               . join (' ', @uninstall)
3151                               . "\n\n");
3152             @uninstall = ('uninstall-recursive', 'uninstall-am');
3153             push (@phony, 'uninstall-am');
3154         }
3155         else
3156         {
3157             @uninstall = ('uninstall-recursive');
3158         }
3159     }
3160
3161     # Step three: print definitions users can use.  Code below knows
3162     # that install-exec is done before install-data, beware.
3163     $output_rules .= ("install-exec: "
3164                       . join (' ', @install_exec)
3165                       . "\n");
3166     $output_rules .= "\t\$(NORMAL_INSTALL)\n";
3167     if (defined $contents{'install-exec-hook'})
3168     {
3169         $output_rules .= "\t" . '$(MAKE) install-exec-hook' . "\n";
3170     }
3171     $output_rules .= "\n";
3172     push (@install, 'install-exec') if !$recursive_install;
3173     push (@phony, 'install-exec');
3174
3175     $output_rules .= ("install-data: "
3176                       . join (' ', @install_data)
3177                       . "\n");
3178     $output_rules .= "\t\$(NORMAL_INSTALL)\n";
3179     if (defined $contents{'install-data-hook'})
3180     {
3181         $output_rules .= "\t" . '$(MAKE) install-data-hook' . "\n";
3182     }
3183     $output_rules .= "\n";
3184     push (@install, 'install-data') if !$recursive_install;
3185     push (@phony, 'install-data');
3186
3187     # If no dependencies for 'install', add 'all'.  Why?  That way
3188     # "make install" at top level of distclean'd distribution won't
3189     # fail because stuff in 'lib' fails to build.
3190     if (! @install || (scalar (@install) == 2
3191                        && $install[0] eq 'install-exec'
3192                        && $install[1] eq 'install-data'))
3193     {
3194         push (@install, 'all');
3195     }
3196     $output_rules .= ('install: '
3197                       . join (' ', @install)
3198                       # Use "@:" as empty command so nothing prints.
3199                       . "\n\t\@:"
3200                       . "\n\n"
3201                       . 'uninstall: '
3202                       . join (' ', @uninstall)
3203                       . "\n\n");
3204     push (@phony, 'install', 'uninstall');
3205
3206     $output_rules .= ('all: '
3207                       . join (' ', @all)
3208                       . "\n\n");
3209     push (@phony, 'all');
3210
3211     # Generate the new 'install-strip' target.
3212     $output_rules .= ("install-strip:\n\t"
3213                       . '$(MAKE) INSTALL_PROGRAM=\'$(INSTALL_PROGRAM) -s\' install'
3214                       . "\n");
3215 }
3216
3217 # Helper for handle_merge_targets.
3218 sub do_one_merge_target
3219 {
3220     local ($name, @values) = @_;
3221
3222     if (defined $contents{$name . '-local'})
3223     {
3224         # User defined local form of target.  So include it.
3225         push (@values, $name . '-local');
3226         push (@phony, $name . '-local');
3227     }
3228
3229     &pretty_print_rule ($name . ":", "\t\t", @values);
3230     push (@phony, $name);
3231 }
3232
3233 # Handle check merge target specially.
3234 sub do_check_merge_target
3235 {
3236     if (defined $contents{'check-local'})
3237     {
3238         # User defined local form of target.  So include it.
3239         push (@check_tests, 'check-local');
3240         push (@phony, 'check-local');
3241     }
3242
3243     if (! &variable_defined ('SUBDIRS'))
3244     {
3245         # 'check' must depend on `all', but not when doing recursive
3246         # build.
3247         unshift (@check, 'all');
3248     }
3249     else
3250     {
3251         # When subdirs are used, do the `all' build and then do all
3252         # the recursive stuff.  Actually use `all-am' because it
3253         # doesn't recurse; we rely on the check target in the subdirs
3254         # to do the required builds there.
3255         unshift (@check, 'all-am');
3256     }
3257
3258     # The check target must depend on the local equivalent of `all',
3259     # to ensure all the primary targets are built.  Also it must
3260     # depend on the test code named in @check.
3261     &pretty_print_rule ('check:', "\t\t", @check);
3262
3263     # Now the check rules must explicitly run anything named in
3264     # @check_tests.  This is done via a separate make invocation to
3265     # avoid problems with parallel makes.  Every time I write code
3266     # like this I wonder: how could you invent a parallel make and not
3267     # provide any real synchronization facilities?
3268     &pretty_print_rule ("\t\$(MAKE)", "\t  ", @check_tests);
3269 }
3270
3271 # Handle all 'clean' targets.
3272 sub handle_clean
3273 {
3274     push (@clean, 'generic');
3275     $output_rules .= &file_contents ('clean');
3276     &push_phony_cleaners ('generic');
3277
3278     local ($target) = $recursive_install ? 'clean-am' : 'clean';
3279     &do_one_clean_target ($target, 'mostly', '', @clean);
3280     &do_one_clean_target ($target, '', 'mostly', @clean);
3281     &do_one_clean_target ($target, 'dist', '', @clean);
3282     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
3283
3284     push (@phony, 'clean', 'mostlyclean', 'distclean', 'maintainer-clean');
3285
3286     local (@deps);
3287     if ($recursive_install)
3288     {
3289         # Do -recursive before -am.  If you aren't doing a parallel
3290         # make, this can be nicer.
3291         @deps = ('recursive', 'am');
3292         &do_one_clean_target ('', 'mostly', '', @deps);
3293         &do_one_clean_target ('', '', '', @deps);
3294         &do_one_clean_target ('', 'dist', '', @deps);
3295         &do_one_clean_target ('', 'maintainer-', '', @deps);
3296     }
3297 }
3298
3299 # Helper for handle_clean.
3300 sub do_one_clean_target
3301 {
3302     local ($target, $name, $last_name, @deps) = @_;
3303
3304     # Special case: if target not passed, then don't generate
3305     # dependency on next "lower" clean target (eg no
3306     # clean<-mostlyclean derivation).  In this case the target is
3307     # implicitly known to be 'clean'.
3308     local ($flag) = $target;
3309     $target = 'clean' if ! $flag;
3310
3311     grep (($_ = $name . 'clean-' . $_) && 0, @deps);
3312     if ($flag)
3313     {
3314         if ($last_name || $name ne 'mostly')
3315         {
3316             push (@deps, $last_name . $target);
3317         }
3318     }
3319
3320     # If a -local version of the rule is given, add it to the list.
3321     if (defined $contents{$name . $target . '-local'})
3322     {
3323         push (@deps, $name . $target . '-local');
3324     }
3325
3326     # Print the target and the dependencies.
3327     &pretty_print_rule ($name . $target . ": ", "\t\t", @deps);
3328
3329     # FIXME: shouldn't we really print these messages before running
3330     # the dependencies?
3331     if ($name . $target eq 'maintainer-clean')
3332     {
3333         # Print a special warning.
3334         $output_rules .=
3335             ("\t\@echo \"This command is intended for maintainers to use;\"\n"
3336              . "\t\@echo \"it deletes files that may require special "
3337              . "tools to rebuild.\"\n");
3338
3339         $output_rules .= "\trm -f config.status\n"
3340             if $relative_dir eq '.';
3341     }
3342     elsif ($name . $target eq 'distclean')
3343     {
3344         $output_rules .= "\trm -f config.status\n";
3345         $output_rules .= "\trm -f libtool\n" if $seen_libtool;
3346     }
3347     $output_rules .= "\n";
3348 }
3349
3350 # Handle .PHONY target.
3351 sub handle_phony
3352 {
3353     &pretty_print_rule ('.PHONY:', "", @phony);
3354     $output_rules .= "\n";
3355 }
3356
3357 # Handle TESTS variable and other checks.
3358 sub handle_tests
3359 {
3360     if (defined $options{'dejagnu'})
3361     {
3362         push (@check_tests, 'check-DEJAGNU');
3363         push (@phony, 'check-DEJAGNU');
3364
3365         local ($xform);
3366         if ($cygnus_mode)
3367         {
3368             $xform = 's/^CYGNUS//;';
3369         }
3370         else
3371         {
3372             $xform = 's/^CYGNUS.*$//;';
3373         }
3374         $output_rules .= &file_contents_with_transform ($xform, 'dejagnu');
3375
3376         # In Cygnus mode, these are found in the build tree.
3377         # Otherwise they are looked for in $PATH.
3378         &define_program_variable ('EXPECT', 'build', 'expect', 'expect');
3379         &define_program_variable ('RUNTEST', 'src', 'dejagnu', 'runtest');
3380
3381         # Note that in the rule we don't directly generate site.exp to
3382         # avoid the possibility of a corrupted site.exp if make is
3383         # interrupted.  Jim Meyering has some useful text on this
3384         # topic.
3385         $output_rules .= ("site.exp: Makefile\n"
3386                           . "\t\@echo 'Making a new site.exp file...'\n"
3387                           . "\t-\@rm -f site.bak\n"
3388                           . "\t\@echo '## these variables are automatically generated by make ##' > \$\@-t\n"
3389                           . "\t\@echo '# Do not edit here.  If you wish to override these values' >> \$\@-t\n"
3390                           . "\t\@echo '# edit the last section' >> \$\@-t\n"
3391                           . "\t\@echo 'set tool \$(DEJATOOL)' >> \$\@-t\n"
3392                           . "\t\@echo 'set srcdir \$(srcdir)' >> \$\@-t\n"
3393                           . "\t\@echo 'set objdir' \`pwd\` >> \$\@-t\n");
3394
3395         # Extra stuff for AC_CANONICAL_*
3396         local (@whatlist) = ();
3397         if ($seen_canonical)
3398         {
3399             push (@whatlist, 'host')
3400         }
3401
3402         # Extra stuff only for AC_CANONICAL_SYSTEM.
3403         if ($seen_canonical == $AC_CANONICAL_SYSTEM)
3404         {
3405             push (@whatlist, 'target', 'build');
3406         }
3407
3408         local ($c1, $c2);
3409         foreach $c1 (@whatlist)
3410         {
3411             foreach $c2 ('alias', 'triplet')
3412             {
3413                 $output_rules .= "\t\@echo 'set ${c1}_${c2} \$(${c1}_${c2})' >> \$\@-t\n";
3414             }
3415         }
3416
3417         $output_rules .= ("\t\@echo '## All variables above are generated by configure. Do Not Edit ##' >> \$\@-t\n"
3418                           . "\t-\@sed '1,/^## All variables above are.*##/ d' site.bak >> \$\@-t\n"
3419                           . "\t-\@mv site.exp site.bak\n"
3420                           . "\t\@mv \$\@-t site.exp\n");
3421     }
3422     else
3423     {
3424         local ($c);
3425         foreach $c ('DEJATOOL', 'RUNTEST', 'RUNTESTFLAGS')
3426         {
3427             if (&variable_defined ($c))
3428             {
3429                 &am_line_error ($c, "\`$c' defined but \`dejagnu' not in \`AUTOMAKE_OPTIONS'");
3430             }
3431         }
3432     }
3433
3434     if (&variable_defined ('TESTS'))
3435     {
3436         push (@check_tests, 'check-TESTS');
3437         push (@phony, 'check-TESTS');
3438
3439         $output_rules .= 'check-TESTS: $(TESTS)
3440         @failed=0; all=0; \\
3441         srcdir=$(srcdir); export srcdir; \\
3442         for tst in $(TESTS); do \\
3443           all=`expr $$all + 1`; \\
3444           if test -f $$tst; then dir=.; \\
3445           else dir="$(srcdir)"; fi; \\
3446           if $(TESTS_ENVIRONMENT) $$dir/$$tst; then \\
3447             echo "PASS: $$tst"; \\
3448           else \\
3449             failed=`expr $$failed + 1`; \\
3450             echo "FAIL: $$tst"; \\
3451           fi; \\
3452         done; \\
3453         if test "$$failed" -eq 0; then \\
3454           echo "========================"; \\
3455           echo "All $$all tests passed"; \\
3456           echo "========================"; \\
3457         else \\
3458           echo "$$failed of $$all tests failed"; \\
3459           exit 1; \\
3460         fi
3461 ';
3462     }
3463 }
3464
3465 # Handle Emacs Lisp.
3466 sub handle_emacs_lisp
3467 {
3468     local (@elfiles) = &am_install_var ('lisp', 'LISP', 'lisp', 'noinst');
3469
3470     if (@elfiles)
3471     {
3472         # Found some lisp.
3473         &define_configure_variable ('lispdir');
3474         &define_configure_variable ('EMACS');
3475         $output_rules .= (".el.elc:\n"
3476                           . "\t\@echo 'WARNING: Warnings can be ignored. :-)'\n"
3477                           . "\tif test \$(EMACS) != no; then \\\n"
3478                           . "\t  EMACS=\$(EMACS) \$(SHELL) \$(srcdir)/elisp-comp \$<; \\\n"
3479                           . "\tfi\n");
3480         push (@suffixes, '.el', '.elc');
3481
3482         # Generate .elc files.
3483         grep ($_ .= 'c', @elfiles);
3484         &define_pretty_variable ('ELCFILES', @elfiles);
3485
3486         push (@clean, 'lisp');
3487         &push_phony_cleaners ('lisp');
3488
3489         push (@all, '$(ELCFILES)');
3490
3491         local ($varname);
3492         if (&variable_defined ('lisp_LISP'))
3493         {
3494             $varname = 'lisp_LISP';
3495             &am_error ("\`lisp_LISP' defined but \`AM_PATH_LISPDIR' not in \`configure.in'")
3496                 if ! $seen_lispdir;
3497         }
3498         else
3499         {
3500             $varname = 'noinst_LISP';
3501         }
3502
3503         &require_file_with_line ($varname, $FOREIGN, 'elisp-comp');
3504     }
3505 }
3506
3507 ################################################################
3508
3509 # Scan one file for interesting things.  Subroutine of scan_configure.
3510 sub scan_one_configure_file
3511 {
3512     local ($filename) = @_;
3513
3514     open (CONFIGURE, $filename)
3515         || die "automake: couldn't open \`$filename': $!\n";
3516     print "automake: reading $filename\n" if $verbose;
3517
3518     while (<CONFIGURE>)
3519     {
3520         # Remove comments from current line.
3521         s/\bdnl\b.*$//;
3522         s/\#.*$//;
3523
3524         # Skip macro definitions.  Otherwise we might be confused into
3525         # thinking that a macro that was only defined was actually
3526         # used.
3527         next if /AC_DEFUN/;
3528
3529         # Populate libobjs array.
3530         if (/AC_FUNC_ALLOCA/)
3531         {
3532             $libsources{'alloca.c'} = 1;
3533         }
3534         elsif (/AC_FUNC_GETLOADAVG/)
3535         {
3536             $libsources{'getloadavg.c'} = 1;
3537         }
3538         elsif (/AC_FUNC_MEMCMP/)
3539         {
3540             $libsources{'memcmp.c'} = 1;
3541         }
3542         elsif (/AC_STRUCT_ST_BLOCKS/)
3543         {
3544             $libsources{'fileblocks.c'} = 1;
3545         }
3546         elsif (/A[CM]_REPLACE_GNU_GETOPT/)
3547         {
3548             $libsources{'getopt.c'} = 1;
3549             $libsources{'getopt1.c'} = 1;
3550         }
3551         elsif (/AM_FUNC_STRTOD/)
3552         {
3553             $libsources{'strtod.c'} = 1;
3554         }
3555         elsif (/AM_WITH_REGEX/)
3556         {
3557             $libsources{'rx.c'} = 1;
3558             $libsources{'rx.h'} = 1;
3559             $libsources{'regex.c'} = 1;
3560             $libsources{'regex.h'} = 1;
3561             $omit_dependencies{'rx.h'} = 1;
3562             $omit_dependencies{'regex.h'} = 1;
3563         }
3564         elsif (/AM_FUNC_MKTIME/)
3565         {
3566             $libsources{'mktime.c'} = 1;
3567         }
3568         elsif (/AM_FUNC_ERROR_AT_LINE/)
3569         {
3570             $libsources{'error.c'} = 1;
3571             $libsources{'error.h'} = 1;
3572         }
3573         elsif (/AM_FUNC_OBSTACK/)
3574         {
3575             $libsources{'obstack.c'} = 1;
3576             $libsources{'obstack.h'} = 1;
3577         }
3578         elsif (/LIBOBJS="(.*)\s+\$LIBOBJS"/
3579                || /LIBOBJS="\$LIBOBJS\s+(.*)"/)
3580         {
3581             foreach $libobj_iter (split (' ', $1))
3582             {
3583                 if ($libobj_iter =~ /^(.*)\.o$/)
3584                 {
3585                     $libsources{$1 . '.c'} = 1;
3586                 }
3587             }
3588         }
3589
3590         if (! $in_ac_replace && s/AC_REPLACE_FUNCS\s*\(\[?//)
3591         {
3592             $in_ac_replace = 1;
3593         }
3594         if ($in_ac_replace)
3595         {
3596             $in_ac_replace = 0 if s/[\]\)].*$//;
3597             # Remove trailing backslash.
3598             s/\\$//;
3599             foreach (split)
3600             {
3601                 # Need to skip empty elements for Perl 4.
3602                 next if $_ eq '';
3603                 $libsources{$_ . '.c'} = 1;
3604             }
3605         }
3606
3607         if (/$obsolete_rx/o)
3608         {
3609             local ($hint) = '';
3610             if ($obsolete_macros{$1})
3611             {
3612                 $hint = '; ' . $obsolete_macros{$1};
3613             }
3614             &am_conf_line_error ($filename, $., "\`$1' is obsolete$hint");
3615         }
3616
3617         # Process the AC_OUTPUT macro.
3618         if (! $in_ac_output && s/AC_OUTPUT\s*\(\[?//)
3619         {
3620             $in_ac_output = 1;
3621             $ac_output_line = $.;
3622         }
3623         if ($in_ac_output)
3624         {
3625             $in_ac_output = 0 if s/[\]\),].*$//;
3626
3627             # Look at potential Makefile.am's.
3628             foreach (split)
3629             {
3630                 next if $_ eq "\\";
3631
3632                 # Handle $local:$input syntax.  Note that we ignore
3633                 # every input file past the first, though we keep
3634                 # those around for later.
3635                 local ($local, $input, @rest) = split (/:/);
3636                 if (! $input)
3637                 {
3638                     $input = $local;
3639                 }
3640                 else
3641                 {
3642                     # FIXME: should be error if .in is missing.
3643                     $input =~ s/\.in$//;
3644                 }
3645
3646                 if (-f $input . '.am')
3647                 {
3648                     # We have a file that automake should generate.
3649                     push (@make_input_list, $input);
3650                     $make_list{$input} = join (':', ($local, @rest));
3651                 }
3652                 else
3653                 {
3654                     # We have a file that automake should cause to be
3655                     # rebuilt, but shouldn't generate itself.
3656                     push (@other_input_files, $_);
3657                 }
3658             }
3659         }
3660
3661         if (/$AC_CONFIG_AUX_DIR_PATTERN/o)
3662         {
3663             @config_aux_path = $1;
3664         }
3665
3666         # Check for ansi2knr.
3667         $am_c_prototypes = 1 if /AM_C_PROTOTYPES/;
3668
3669         # Check for Cygwin32.
3670         if (/AM_CYGWIN32/)
3671         {
3672             $seen_cygwin32 = 1;
3673             $configure_vars{'EXEEXT'} = 1;
3674         }
3675
3676         # Check for NLS support.
3677         if (/AM_GNU_GETTEXT/)
3678         {
3679             $seen_gettext = 1;
3680             $ac_gettext_line = $.;
3681             $omit_dependencies{'libintl.h'} = 1;
3682         }
3683
3684         # Look for ALL_LINGUAS.
3685         if (/ALL_LINGUAS="(.*)"$/ || /ALL_LINGUAS=(.*)$/)
3686         {
3687             $seen_linguas = 1;
3688             $all_linguas = $1;
3689             $all_linguas_line = $.;
3690         }
3691
3692         # Handle configuration headers.  A config header of `[$1]'
3693         # means we are actually scanning AM_CONFIG_HEADER from
3694         # aclocal.m4.
3695         if (/A([CM])_CONFIG_HEADER\s*\((.*)\)/
3696             && $2 ne '[$1]')
3697         {
3698             &am_conf_line_error
3699                 ($filename, $.,
3700                  "\`AC_CONFIG_HEADER' is obsolete; use \`AM_CONFIG_HEADER'")
3701                     if $1 eq 'C';
3702
3703             $config_header_line = $.;
3704             $config_name = $2;
3705             if ($config_name =~ /^([^:]+):(.+)$/)
3706             {
3707                 $config_name = $1;
3708                 $config_header = $2;
3709             }
3710             else
3711             {
3712                 $config_header = $config_name . '.in';
3713             }
3714         }
3715
3716         # Handle AC_CANONICAL_*.  Always allow upgrading to
3717         # AC_CANONICAL_SYSTEM, but never downgrading.
3718         $seen_canonical = $AC_CANONICAL_HOST
3719             if ! $seen_canonical
3720                 && (/AC_CANONICAL_HOST/ || /AC_CHECK_TOOL/);
3721         $seen_canonical = $AC_CANONICAL_SYSTEM if /AC_CANONICAL_SYSTEM/;
3722
3723         $seen_path_xtra = 1 if /AC_PATH_XTRA/;
3724
3725         # This macro handles several different things.
3726         if (/$AM_INIT_AUTOMAKE_PATTERN/o)
3727         {
3728             $seen_make_set = 1;
3729             $seen_package = 1;
3730             $seen_version = 1;
3731             $seen_arg_prog = 1;
3732             $seen_prog_install = 2;
3733             ($package_version = $1) =~ s/$AM_PACKAGE_VERSION_PATTERN/$1/o;
3734             $package_version_line = $.;
3735         }
3736
3737         # This is like AM_INIT_AUTOMAKE, but doesn't actually set the
3738         # package and version number.  (This might change in the
3739         # future).  Yes, I'm not above hacking Automake so it works
3740         # well with other GNU tools -- that is actually the point.
3741         if (/AM_INIT_GUILE_MODULE/)
3742         {
3743             $seen_make_set = 1;
3744             $seen_package = 1;
3745             $seen_version = 1;
3746             $seen_arg_prog = 1;
3747             $seen_prog_install = 2;
3748             @config_aux_path = ('..');
3749         }
3750
3751         # Some things required by Automake.
3752         $seen_make_set = 1 if /AC_PROG_MAKE_SET/;
3753         $seen_arg_prog = 1 if /AC_ARG_PROGRAM/;
3754
3755         if (/AC_PROG_(YACC|RANLIB|CC|CXX|LEX)/)
3756         {
3757             $configure_vars{$1} = 1;
3758         }
3759         if (/$AC_CHECK_PATTERN/o)
3760         {
3761             $configure_vars{$3} = 1;
3762         }
3763
3764         # Explicitly avoid ANSI2KNR -- we AC_SUBST that in protos.m4,
3765         # but later define it elsewhere.  This is pretty hacky.
3766         if (/$AC_SUBST_PATTERN/o && $1 ne 'ANSI2KNR')
3767         {
3768             $configure_vars{$1} = 1;
3769         }
3770
3771         $seen_decl_yytext = 1 if /AC_DECL_YYTEXT/;
3772         $seen_maint_mode = 1 if /AM_MAINTAINER_MODE/;
3773         $seen_package = 1 if /PACKAGE=/;
3774
3775         # Skip VERSION of [$2]; that is from AM_INIT_AUTOMAKE.
3776         if (/\bVERSION=(\S+)/ && $1 ne '[$2]')
3777         {
3778             $seen_version = 1;
3779             $package_version = $1;
3780             $package_version_line = $.;
3781         }
3782         elsif (/VERSION=/)
3783         {
3784             $seen_version = 1;
3785         }
3786
3787         # Weird conditionals here because it is always allowed to
3788         # upgrade to AM_PROG_INSTALL but never to downgrade to
3789         # AC_PROG_INSTALL.
3790         $seen_prog_install = 1 if ! $seen_prog_install && /AC_PROG_INSTALL/;
3791         $seen_prog_install = 2 if /AM_PROG_INSTALL/;
3792
3793         $seen_lispdir = 1 if /AM_PATH_LISPDIR/;
3794
3795         if (/AM_PROG_LIBTOOL/)
3796         {
3797             $seen_libtool = 1;
3798             $libtool_line = $.;
3799             $configure_vars{'LIBTOOL'} = 1;
3800             $configure_vars{'RANLIB'} = 1;
3801             $configure_vars{'CC'} = 1;
3802             # AM_PROG_LIBTOOL runs AC_CANONICAL_HOST.  Make sure we
3803             # never downgrade (if we've seen AC_CANONICAL_SYSTEM).
3804             $seen_canonical = $AC_CANONICAL_HOST if ! $seen_canonical;
3805         }
3806     }
3807
3808     close (CONFIGURE);
3809 }
3810
3811 # Scan configure.in and aclocal.m4 for interesting things.  We must
3812 # scan aclocal.m4 because there might be AC_SUBSTs and such there.
3813 sub scan_configure
3814 {
3815     # Reinitialize libsources here.  This isn't really necessary,
3816     # since we currently assume there is only one configure.in.  But
3817     # that won't always be the case.
3818     %libsources = ();
3819
3820     local ($in_ac_output, $in_ac_replace) = (0, 0);
3821     local (%make_list, @make_input_list);
3822     local ($libobj_iter);
3823
3824     &scan_one_configure_file ('configure.in');
3825     &scan_one_configure_file ('aclocal.m4')
3826         if -f 'aclocal.m4';
3827
3828     # Set input and output files if not specified by user.
3829     if (! @input_files)
3830     {
3831         @input_files = @make_input_list;
3832         %output_files = %make_list;
3833     }
3834
3835     &am_conf_error ("\`PACKAGE' not defined in configure.in")
3836         if ! $seen_package;
3837     &am_conf_error ("\`VERSION' not defined in configure.in")
3838         if ! $seen_version;
3839
3840     # Look for some files we need.  Always check for these.  This
3841     # check must be done for every run, even those where we are only
3842     # looking at a subdir Makefile.  We must set relative_dir so that
3843     # the file-finding machinery works.
3844     local ($relative_dir) = '.';
3845     &require_config_file ($FOREIGN, 'install-sh', 'mkinstalldirs', 'missing');
3846     &am_error ("\`install.sh' is an anachronism; use \`install-sh' instead")
3847         if -f $config_aux_path[0] . '/install.sh';
3848 }
3849
3850 ################################################################
3851
3852 # Set up for Cygnus mode.
3853 sub check_cygnus
3854 {
3855     return unless $cygnus_mode;
3856
3857     &set_strictness ('foreign');
3858     $options{'no-installinfo'} = 1;
3859     $options{'no-dependencies'} = 1;
3860     $use_dependencies = 0;
3861
3862     if (! $seen_maint_mode)
3863     {
3864         &am_conf_error ("\`AM_MAINTAINER_MODE' required when --cygnus specified");
3865     }
3866
3867     if (! $seen_cygwin32)
3868     {
3869         &am_conf_error ("\`AM_CYGWIN32' required when --cygnus specified");
3870     }
3871 }
3872
3873 # Do any extra checking for GNU standards.
3874 sub check_gnu_standards
3875 {
3876     if ($relative_dir eq '.')
3877     {
3878         # In top level (or only) directory.
3879         &require_file ($GNU, 'INSTALL', 'NEWS', 'README', 'COPYING',
3880                        'AUTHORS', 'ChangeLog');
3881     }
3882
3883     if ($strictness >= $GNU)
3884     {
3885         if (defined $options{'no-installman'})
3886         {
3887             &am_line_error ('AUTOMAKE_OPTIONS',
3888                             "option \`no-installman' disallowed by GNU standards");
3889         }
3890
3891         if (defined $options{'no-installinfo'})
3892         {
3893             &am_line_error ('AUTOMAKE_OPTIONS',
3894                             "option \`no-installinfo' disallowed by GNU standards");
3895         }
3896     }
3897 }
3898
3899 # Do any extra checking for GNITS standards.
3900 sub check_gnits_standards
3901 {
3902     if ($strictness >= $GNITS)
3903     {
3904         if (-f $relative_dir . '/COPYING.LIB')
3905         {
3906             &am_error ("\`${relative_dir}/COPYING.LIB' disallowed by Gnits standards");
3907         }
3908
3909         if ($relative_dir eq '.')
3910         {
3911             if ($package_version !~ /^$GNITS_VERSION_PATTERN$/)
3912             {
3913                 # FIXME: allow real filename.
3914                 &am_conf_line_error ('configure.in',
3915                                      $package_version_line,
3916                                      "version \`$package_version' doesn't follow Gnits standards");
3917             }
3918             elsif (defined $1 && -f 'README-alpha')
3919             {
3920                 # This means we have an alpha release.  See
3921                 # GNITS_VERSION_PATTERN for details.
3922                 &require_file ($GNITS, 'README-alpha');
3923             }
3924         }
3925     }
3926
3927     if ($relative_dir eq '.')
3928     {
3929         # In top level (or only) directory.
3930         &require_file ($GNITS, 'THANKS');
3931     }
3932 }
3933
3934 ################################################################
3935
3936 # Pretty-print something.  HEAD is what should be printed at the
3937 # beginning of the first line, FILL is what should be printed at the
3938 # beginning of every subsequent line.
3939 sub pretty_print_internal
3940 {
3941     local ($head, $fill, @values) = @_;
3942
3943     local ($column) = length ($head);
3944     local ($result) = $head;
3945
3946     # Fill length is number of characters.  However, each Tab
3947     # character counts for eight.  So we count the number of Tabs and
3948     # multiply by 7.
3949     local ($fill_length) = length ($fill);
3950     $fill_length += 7 * ($fill =~ tr/\t/\t/d);
3951
3952     local ($bol) = ($head eq '');
3953     foreach (@values)
3954     {
3955         # "71" because we also print a space.
3956         if ($column + length ($_) > 71)
3957         {
3958             $result .= " \\\n" . $fill;
3959             $column = $fill_length;
3960             $bol = 1;
3961         }
3962
3963         $result .= ' ' unless ($bol);
3964         $result .= $_;
3965         $column += length ($_) + 1;
3966         $bol = 0;
3967     }
3968
3969     $result .= "\n";
3970     return $result;
3971 }
3972
3973 # Pretty-print something and append to output_vars.
3974 sub pretty_print
3975 {
3976     $output_vars .= &pretty_print_internal (@_);
3977 }
3978
3979 # Pretty-print something and append to output_rules.
3980 sub pretty_print_rule
3981 {
3982     $output_rules .= &pretty_print_internal (@_);
3983 }
3984
3985
3986 ################################################################
3987
3988 # See if a target exists.
3989 sub target_defined
3990 {
3991     local ($target) = @_;
3992     return defined $targets{$target};
3993 }
3994
3995 # See if a variable exists.
3996 sub variable_defined
3997 {
3998     local ($var) = @_;
3999     if (defined $targets{$var})
4000     {
4001         &am_line_error ($var, "\`$var' is target; expected variable");
4002         return 0;
4003     }
4004     elsif (defined $contents{$var})
4005     {
4006         $content_seen{$var} = 1;
4007         return 1;
4008     }
4009     return 0;
4010 }
4011
4012 # Mark a variable as examined.
4013 sub examine_variable
4014 {
4015     local ($var) = @_;
4016     &variable_defined ($var);
4017 }
4018
4019 # Return contents of variable as list, split as whitespace.  This will
4020 # recursively follow $(...) and ${...} inclusions.  It preserves @...@
4021 # substitutions.  If PARENT is specified, it is the name of the
4022 # including variable; this is only used for error reports.
4023 sub variable_value_as_list
4024 {
4025     local ($var, $parent) = @_;
4026     local (@result);
4027
4028     if (defined $targets{$var})
4029     {
4030         &am_line_error ($var, "\`$var' is target; expected variable");
4031     }
4032     elsif (! defined $contents{$var})
4033     {
4034         &am_line_error ($parent, "variable \`$var' not defined");
4035     }
4036     else
4037     {
4038         local (@temp_list);
4039         $content_seen{$var} = 1;
4040         foreach (split (' ', $contents{$var}))
4041         {
4042             # If a comment seen, just leave.
4043             last if /^#/;
4044
4045             # Handle variable substitutions.
4046             if (/^\$\{(.*)\}$/ || /^\$\((.*)\)$/)
4047             {
4048                 local ($varname) = $1;
4049                 local ($from, $to);
4050                 if ($varname =~ /^([^:]*):([^=]*)=(.*)$/)
4051                 {
4052                     $varname = $1;
4053                     $to = $3;
4054                     ($from = $2) =~ s/(\W)/\\$1/g;
4055                 }
4056
4057                 # Find the value.
4058                 @temp_list = &variable_value_as_list ($1, $var);
4059
4060                 # Now rewrite the value if appropriate.
4061                 if ($from)
4062                 {
4063                     grep (s/$from$/$to/, @temp_list);
4064                 }
4065
4066                 push (@result, @temp_list);
4067             }
4068             else
4069             {
4070                 push (@result, $_);
4071             }
4072         }
4073     }
4074
4075     return @result;
4076 }
4077
4078 # Define a new variable, but only if not already defined.
4079 sub define_variable
4080 {
4081     local ($var, $value) = @_;
4082
4083     if (! defined $contents{$var})
4084     {
4085         $output_vars .= $var . ' = ' . $value . "\n";
4086         $contents{$var} = $value;
4087         $content_seen{$var} = 1;
4088     }
4089 }
4090
4091 # Like define_variable, but second arg is a list, and is
4092 # pretty-printed.
4093 sub define_pretty_variable
4094 {
4095     local ($var, @value) = @_;
4096     if (! defined $contents{$var})
4097     {
4098         $contents{$var} = join (' ', @value);
4099         &pretty_print ($var . ' = ', '', @value);
4100         $content_seen{$var} = 1;
4101     }
4102 }
4103
4104 # Like define_variable, but define a variable to be the configure
4105 # substitution by the same name.
4106 sub define_configure_variable
4107 {
4108     local ($var) = @_;
4109     local ($value) = '@' . $var . '@';
4110     &define_variable ($var, $value);
4111 }
4112
4113 # Define a variable that represents a program to run.  If in Cygnus
4114 # mode, the program is searched for in the build (or source) tree.
4115 # Otherwise no searching is done at all.  Arguments are:
4116 # * VAR      Name of variable to define
4117 # * WHATDIR  Either `src' or `build', depending on where program should
4118 #            be found.  (runtest is in srcdir!)
4119 # * SUBDIR   Subdir of top-level dir
4120 # * PROGRAM  Name of program
4121 sub define_program_variable
4122 {
4123     local ($var, $whatdir, $subdir, $program) = @_;
4124
4125     if ($cygnus_mode)
4126     {
4127         local ($full) = ('$(top_' . $whatdir . 'dir)/../'
4128                          . $subdir . '/' . $program);
4129         &define_variable ($var, ('`if test -f ' . $full
4130                                  . '; then echo ' . $full . '; else echo '
4131                                  . $program . '; fi`'));
4132     }
4133     else
4134     {
4135         &define_variable ($var, $program);
4136     }
4137 }
4138
4139
4140 ################################################################
4141
4142 # Read Makefile.am and set up %contents.  Simultaneously copy lines
4143 # from Makefile.am into $output_trailer or $output_vars as
4144 # appropriate.  NOTE we put rules in the trailer section.  We want
4145 # user rules to come after our generated stuff.
4146 sub read_am_file
4147 {
4148     local ($amfile) = @_;
4149
4150     open (AM_FILE, $amfile) || die "automake: couldn't open \`$amfile': $!\n";
4151     print "automake: reading $amfile\n" if $verbose;
4152
4153     $output_vars = ("# $in_file_name generated automatically by automake "
4154                     . $VERSION . " from $am_file_name\n");
4155
4156     # Generate copyright for generated Makefile.in.
4157     $output_vars .= $gen_copyright;
4158
4159     local ($saw_bk) = 0;
4160     local ($was_rule) = 0;
4161     local ($spacing) = '';
4162     local ($comment) = '';
4163     local ($last_var_name) = '';
4164     local ($blank) = 0;
4165
4166     while (<AM_FILE>)
4167     {
4168         if (/$IGNORE_PATTERN/o)
4169         {
4170             # Merely delete comments beginning with two hashes.
4171         }
4172         elsif (/$WHITE_PATTERN/o)
4173         {
4174             # Stick a single white line before the incoming macro or rule.
4175             $spacing = "\n";
4176             $blank = 1;
4177         }
4178         elsif (/$COMMENT_PATTERN/o)
4179         {
4180             # Stick comments before the incoming macro or rule.  Make
4181             # sure a blank line preceeds first block of comments.
4182             $spacing = "\n" unless $blank;
4183             $blank = 1;
4184             $comment .= $spacing . $_;
4185             $spacing = '';
4186         }
4187         else
4188         {
4189             last;
4190         }
4191     }
4192
4193     $output_vars .= $comment . "\n";
4194     $comment = '';
4195     $spacing = "\n";
4196     local ($am_vars) = '';
4197
4198     local ($is_ok_macro);
4199     while ($_)
4200     {
4201         $_ .= "\n"
4202             unless substr ($_, -1, 1) eq "\n";
4203
4204         $_ =~ s/\@MAINT\@//g
4205             unless $seen_maint_mode;
4206
4207         if (/$IGNORE_PATTERN/o)
4208         {
4209             # Merely delete comments beginning with two hashes.
4210         }
4211         elsif (/$WHITE_PATTERN/o)
4212         {
4213             # Stick a single white line before the incoming macro or rule.
4214             $spacing = "\n";
4215         }
4216         elsif (/$COMMENT_PATTERN/o)
4217         {
4218             # Stick comments before the incoming macro or rule.
4219             $comment .= $spacing . $_;
4220             $spacing = '';
4221         }
4222         elsif ($saw_bk)
4223         {
4224             if ($was_rule)
4225             {
4226                 $output_trailer .= $_;
4227                 $saw_bk = /\\$/;
4228             }
4229             else
4230             {
4231                 $am_vars .= $_;
4232                 $saw_bk = /\\$/;
4233                 # Chop newline and backslash if this line is
4234                 # continued.  FIXME: maybe ensure trailing whitespace
4235                 # exists?
4236                 chop if $saw_bk;
4237                 chop if $saw_bk;
4238                 $contents{$last_var_name} .= $_;
4239             }
4240         }
4241         elsif (/$RULE_PATTERN/o)
4242         {
4243             # warn "** Saw rule .$1.\n";
4244             # Found a rule.
4245             $was_rule = 1;
4246             # Value here doesn't matter; for targets we only note
4247             # existence.
4248             $contents{$1} = 1;
4249             $targets{$1} = 1;
4250             $content_lines{$1} = $.;
4251             $output_trailer .= $comment . $spacing . $_;
4252             $comment = $spacing = '';
4253             $saw_bk = /\\$/;
4254         }
4255         elsif (($is_ok_macro = /$MACRO_PATTERN/o)
4256                || /$BOGUS_MACRO_PATTERN/o)
4257         {
4258             # Found a macro definition.
4259             $was_rule = 0;
4260             $last_var_name = $1;
4261             if ($2 ne '' && substr ($2, -1) eq "\\")
4262             {
4263                 $contents{$1} = substr ($2, 0, length ($2) - 1);
4264             }
4265             else
4266             {
4267                 $contents{$1} = $2;
4268             }
4269             $content_lines{$1} = $.;
4270             $am_vars .= $comment . $spacing . $_;
4271             $comment = $spacing = '';
4272             $saw_bk = /\\$/;
4273
4274             # Error if bogus.
4275             &am_line_error ($., "bad macro name \`$1'")
4276                 if ! $is_ok_macro;
4277         }
4278         else
4279         {
4280             # This isn't an error; it is probably a continued rule.
4281             # In fact, this is what we assume.
4282             $was_rule = 1;
4283             $output_trailer .= $comment . $spacing . $_;
4284             $comment = $spacing = '';
4285             $saw_bk = /\\$/;
4286         }
4287
4288         $_ = <AM_FILE>;
4289     }
4290
4291     $output_trailer .= $comment;
4292
4293     # Compute relative location of the top object directory.
4294     local (@topdir) = ();
4295     foreach (split (/\//, $relative_dir))
4296     {
4297         next if $_ eq '.' || $_ eq '';
4298         if ($_ eq '..')
4299         {
4300             pop @topdir;
4301         }
4302         else
4303         {
4304             push (@topdir, '..');
4305         }
4306     }
4307     @topdir = ('.') if ! @topdir;
4308
4309     $top_builddir = join ('/', @topdir);
4310     local ($build_rx);
4311     ($build_rx = $top_builddir) =~ s/(\W)/\\$1/g;
4312     $output_vars .= &file_contents_with_transform
4313                         ('s/\@top_builddir\@/' . $build_rx . '/g;',
4314                          'header-vars');
4315
4316     # Generate some useful variables when AC_CANONICAL_* used.  FIXME:
4317     # this should use generic %configure_vars method.
4318     if ($seen_canonical)
4319     {
4320         local ($curs, %vars);
4321         $vars{'host_alias'} = 'host_alias';
4322         $vars{'host_triplet'} = 'host';
4323         if ($seen_canonical == $AC_CANONICAL_SYSTEM)
4324         {
4325             $vars{'build_alias'} = 'build_alias';
4326             $vars{'build_triplet'} = 'build';
4327             $vars{'target_alias'} = 'target_alias';
4328             $vars{'target_triplet'} = 'target';
4329         }
4330         foreach $curs (sort keys %vars)
4331         {
4332             $output_vars .= "$curs = \@$vars{$curs}\@\n";
4333             $contents{$curs} = "\@$vars{$curs}\@";
4334         }
4335     }
4336
4337     local ($curs);
4338     foreach $curs (sort keys %configure_vars)
4339     {
4340         &define_configure_variable ($curs);
4341     }
4342
4343     $output_vars .= $am_vars;
4344 }
4345
4346 ################################################################
4347
4348 sub initialize_global_constants
4349 {
4350     # Values for AC_CANONICAL_*
4351     $AC_CANONICAL_HOST = 1;
4352     $AC_CANONICAL_SYSTEM = 2;
4353
4354     # Associative array of standard directory names.  Entry is TRUE if
4355     # corresponding directory should be installed during
4356     # 'install-exec' phase.
4357     %exec_dir_p =
4358         ('bin', 1,
4359          'sbin', 1,
4360          'libexec', 1,
4361          'data', 0,
4362          'sysconf', 1,
4363          'localstate', 1,
4364          'lib', 1,
4365          'info', 0,
4366          'man', 0,
4367          'include', 0,
4368          'oldinclude', 0,
4369          'pkgdata', 0,
4370          'pkglib', 1,
4371          'pkginclude', 0
4372          );
4373
4374     # Helper text for dealing with man pages.
4375     $install_man_format =
4376     '   @sect=@SECTION@;                                \\
4377         inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
4378         if test -f $(srcdir)/@MAN@; then file=$(srcdir)/@MAN@; \\
4379         else file=@MAN@; fi; \\
4380         echo " $(INSTALL_DATA) $$file $(mandir)/man$$sect/$$inst"; \\
4381         $(INSTALL_DATA) $$file $(mandir)/man$$sect/$$inst
4382 ';
4383
4384     $uninstall_man_format =
4385     '   inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
4386         rm -f $(mandir)/man@SECTION@/$$inst
4387 ';
4388
4389     # Commonly found files we look for and automatically include in
4390     # DISTFILES.
4391     @common_files =
4392         (
4393          "README", "THANKS", "TODO", "NEWS", "COPYING", "COPYING.LIB",
4394          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
4395          "config.guess", "config.sub", "AUTHORS", "BACKLOG", "ABOUT-GNU",
4396          "libversion.in", "mdate-sh", "mkinstalldirs", "install-sh",
4397          'texinfo.tex', "ansi2knr.c", "ansi2knr.1", 'elisp-comp',
4398          'interlock', 'ylwrap', 'acinclude.m4', @libtoolize_files,
4399          'missing'
4400          );
4401
4402     # Commonly used files we auto-include, but only sometimes.
4403     @common_sometimes =
4404         (
4405          "aclocal.m4", "acconfig.h", "config.h.top",
4406          "config.h.bot", "stamp-h.in", 'stamp-vti'
4407          );
4408
4409     $USAGE = "\
4410   -a, --add-missing     add missing standard files to package
4411   --amdir=DIR           directory storing config files
4412   --build-dir=DIR       directory where build being done (for dependencies)
4413   --cygnus              assume program is part of Cygnus-style tree
4414   --foreign             set strictness to foreign
4415   --gnits               set strictness to gnits
4416   --gnu                 set strictness to gnu
4417   --help                print this help, then exit
4418   -i, --include-deps    include generated dependencies in Makefile.in
4419   --no-force            only update Makefile.in's that are out of date
4420   -o DIR, --output-dir=DIR
4421                         put generated Makefile.in's into DIR
4422   --srcdir-name=DIR     name used for srcdir (for dependencies)
4423   -v, --verbose         verbosely list files processed
4424   --version             print version number, then exit\n";
4425
4426     # Copyright on generated Makefile.ins.
4427     $gen_copyright = "\
4428 # Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
4429 # This Makefile.in is free software; the Free Software Foundation
4430 # gives unlimited permission to copy, distribute and modify it.
4431 ";
4432
4433     # Ignore return result from chmod, because it might give an error
4434     # if we chmod a symlink.
4435     $dist_header = "\t" . '-chmod -R a+r $(distdir)' . "\n";
4436     $dist{'tarZ'} = ("\t"
4437                      . '$(TAR) chf - $(distdir) | compress -c > $(distdir).tar.Z'
4438                      . "\n");
4439     $dist{'shar'} = ("\t"
4440                      . 'shar $(distdir) | gzip > $(distdir).shar.gz'
4441                      . "\n");
4442     $dist{'zip'} = "\t" . 'zip -rq $(distdir).zip $(distdir)' . "\n";
4443     $dist{'dist'} = "\t" .  'GZIP=$(GZIP) $(TAR) chozf $(distdir).tar.gz $(distdir)' . "\n";
4444     $dist_trailer = "\t" . 'rm -rf $(distdir)' . "\n";
4445 }
4446
4447 # (Re)-Initialize per-Makefile.am variables.
4448 sub initialize_per_input
4449 {
4450     # These two variables are used when generating each Makefile.in.
4451     # They hold the Makefile.in until it is ready to be printed.
4452     $output_rules = '';
4453     $output_vars = '';
4454     $output_trailer = '';
4455     $output_header = '';
4456
4457     # Suffixes found during a run.
4458     @suffixes = ();
4459
4460     # This holds the contents of a Makefile.am, as parsed by
4461     # read_am_file.
4462     %contents = ();
4463
4464     # This holds the names which are targets.  These also appear in
4465     # %contents.
4466     %targets = ();
4467
4468     # This holds the line numbers at which various elements of
4469     # %contents are defined.
4470     %content_lines = ();
4471
4472     # This holds a 1 if a particular variable was examined.
4473     %content_seen = ();
4474
4475     # This holds the "relative directory" of the current Makefile.in.
4476     # Eg for src/Makefile.in, this is "src".
4477     $relative_dir = '';
4478
4479     # This holds a list of files that are included in the
4480     # distribution.
4481     %dist_common = ();
4482
4483     # List of dependencies for the obvious targets.
4484     @install_data = ();
4485     @install_exec = ();
4486     @uninstall = ();
4487     @installdirs = ();
4488
4489     @info = ();
4490     @dvi = ();
4491     @all = ();
4492     @check = ();
4493     @check_tests = ();
4494     @installcheck = ();
4495     @clean = ();
4496
4497     @phony = ();
4498
4499     # These are pretty obvious, too.  They are used to define the
4500     # SOURCES and OBJECTS variables.
4501     @sources = ();
4502     @objects = ();
4503
4504     # TRUE if current directory holds any C source files.
4505     $dir_holds_sources = 0;
4506
4507     # These variables track inclusion of various compile-related .am
4508     # files.  $included_generic_compile is TRUE if the basic code has
4509     # been included.  $included_knr_compile is TRUE if the ansi2knr
4510     # code has been included.  $included_libtool_compile is TRUE if
4511     # libtool support has been included.
4512     $included_generic_compile = 0;
4513     $included_knr_compile = 0;
4514     $included_libtool_compile = 0;
4515
4516     # TRUE if current directory holds any headers.
4517     $dir_holds_headers = 0;
4518
4519     # TRUE if install targets should work recursively.
4520     $recursive_install = 0;
4521
4522     # All .P files.
4523     %dep_files = ();
4524
4525     # Strictness levels.
4526     $strictness = $default_strictness;
4527     $strictness_name = $default_strictness_name;
4528
4529     # Options from AUTOMAKE_OPTIONS.
4530     %options = ();
4531
4532     # Whether or not dependencies are handled.  Can be further changed
4533     # in handle_options.
4534     $use_dependencies = $cmdline_use_dependencies;
4535
4536     # Per Makefile.am.
4537     $local_maint_charset = $maint_charset;
4538
4539     # All yacc and lex source filenames for this directory.  Use
4540     # filenames instead of raw count so that multiple instances are
4541     # counted correctly (eg one yacc file can appear in multiple
4542     # programs without harm).
4543     %yacc_sources = ();
4544     %lex_sources = ();
4545
4546     # C++ source extensions we've seen.
4547     %cxx_extensions = ();
4548
4549     # TRUE if we've seen any non-C++ sources.  This actually holds a
4550     # line number or the name of a symbol corresponding to a line
4551     # number where the C sources were seen.  If it is -1 then it means
4552     # we couldn't (easily) figure out which line of the Makefile.am
4553     # mentioned the sources.
4554     $seen_c_source = 0;
4555
4556     # This is a list of all targets to run during "make dist".
4557     @dist_targets = ();
4558
4559     # Keys in this hash are the names of ._o files which must depend
4560     # on ansi2knr.  Ugh.
4561     %de_ansi_objects = ();
4562 }
4563
4564
4565 ################################################################
4566
4567 # Return contents of a file from $am_dir, automatically skipping
4568 # macros or rules which are already known.  Runs command on each line
4569 # as it is read; this command can modify $_.
4570 sub file_contents_with_transform
4571 {
4572     local ($command, $basename) = @_;
4573     local ($file) = $am_dir . '/' . $basename . '.am';
4574
4575     if ($command ne '' && substr ($command, -1) ne ';')
4576     {
4577         die "automake: programming error in file_contents_with_transform\n";
4578     }
4579
4580     open (FC_FILE, $file)
4581         || die "automake: installation error: cannot open \`$file'\n";
4582     # Looks stupid?
4583     # print "automake: reading $file\n" if $verbose;
4584
4585     local ($was_rule) = 0;
4586     local ($result_vars) = '';
4587     local ($result_rules) = '';
4588     local ($comment) = '';
4589     local ($spacing) = "\n";
4590     local ($skipping) = 0;
4591     local ($had_chars);
4592
4593     while (<FC_FILE>)
4594     {
4595         $_ =~ s/\@MAINT\@//g
4596             unless $seen_maint_mode;
4597
4598         $had_chars = length ($_) && $_ ne "\n";
4599         eval $command;
4600         # If the transform caused all the characters to go away, then
4601         # ignore the line.  Why do this?  Because in Perl 4, a "next"
4602         # inside of an eval doesn't affect a loop outside the eval.
4603         # So we can't pass in a "transform" that uses next.  We used
4604         # to do this.  "Empty" also means consisting of a single
4605         # newline.
4606         next if $had_chars && ($_ eq '' || $_ eq "\n");
4607
4608         if (/$IGNORE_PATTERN/o)
4609         {
4610             # Merely delete comments beginning with two hashes.
4611         }
4612         elsif (/$WHITE_PATTERN/o)
4613         {
4614             # Stick a single white line before the incoming macro or rule.
4615             $spacing = "\n";
4616         }
4617         elsif (/$COMMENT_PATTERN/o)
4618         {
4619             # Stick comments before the incoming macro or rule.
4620             $comment .= $spacing . $_;
4621             $spacing = '';
4622         }
4623         elsif ($saw_bk)
4624         {
4625             if ($was_rule)
4626             {
4627                 $result_rules .= $_ if ! $skipping;
4628             }
4629             else
4630             {
4631                 $result_vars .= $_ if ! $skipping;
4632             }
4633             $saw_bk = /\\$/;
4634         }
4635         elsif (/$RULE_PATTERN/o)
4636         {
4637             # warn "** Found rule .$1.\n";
4638             # Found a rule.
4639             $was_rule = 1;
4640             $skipping = defined $contents{$1};
4641             # warn "** Skip $skipping\n" if $skipping;
4642             $result_rules .= $comment . $spacing . $_ if ! $skipping;
4643             $comment = $spacing = '';
4644             $saw_bk = /\\$/;
4645         }
4646         elsif (/$MACRO_PATTERN/o)
4647         {
4648             # warn "** Found macro .$1.\n";
4649             # Found a variable reference.
4650             $was_rule = 0;
4651             $skipping = defined $contents{$1};
4652             # warn "** Skip $skipping\n" if $skipping;
4653             $result_vars .= $comment . $spacing . $_ if ! $skipping;
4654             $comment = $spacing = '';
4655             $saw_bk = /\\$/;
4656         }
4657         else
4658         {
4659             # This isn't an error; it is probably a continued rule.
4660             # In fact, this is what we assume.
4661             $was_rule = 1;
4662             $result_rules .= $comment . $spacing . $_ if ! $skipping;
4663             $comment = $spacing = '';
4664             $saw_bk = /\\$/;
4665         }
4666     }
4667
4668     close (FC_FILE);
4669     return $result_vars . $result_rules . $comment;
4670 }
4671
4672 # Like file_contents_with_transform, but no transform.
4673 sub file_contents
4674 {
4675     return &file_contents_with_transform ('', @_);
4676 }
4677
4678 # Find all variable prefixes that are used for install directories.  A
4679 # prefix `zar' qualifies iff:
4680 # * `zardir' is a variable.
4681 # * `zar_PRIMARY' is a variable.
4682 sub am_primary_prefixes
4683 {
4684     local ($primary, @prefixes) = @_;
4685
4686     local (%valid, $varname);
4687     grep ($valid{$_} = 0, @prefixes);
4688     $valid{'EXTRA'} = 0;
4689     foreach $varname (keys %contents)
4690     {
4691         if ($varname =~ /^(.*)_$primary$/)
4692         {
4693             if (! defined $valid{$1}
4694                 && ! &variable_defined ($1 . 'dir')
4695                 # Note that a configure variable is always legitimate.
4696                 # It is natural to name such variables after the
4697                 # primary, so we explicitly allow it.
4698                 && ! defined $configure_vars{$varname})
4699             {
4700                 &am_line_error ($varname, "invalid variable \"$varname\"");
4701             }
4702             else
4703             {
4704                 # Ensure all extended prefixes are actually used.
4705                 $valid{$1} = 1;
4706             }
4707         }
4708     }
4709
4710     return %valid;
4711 }
4712
4713 # Handle `where_HOW' variable magic.  Does all lookups, generates
4714 # install code, and possibly generates code to define the primary
4715 # variable.  The first argument is the name of the .am file to munge,
4716 # the second argument is the primary variable (eg HEADERS), and all
4717 # subsequent arguments are possible installation locations.  Returns
4718 # list of all values of all _HOW targets.
4719 #
4720 # FIXME: this should be rewritten to be cleaner.  It should be broken
4721 # up into multiple functions.
4722 #
4723 # Usage is: am_install_var (OPTION..., file, HOW, where...)
4724 sub am_install_var
4725 {
4726     local (@args) = @_;
4727
4728     local ($do_clean) = 0;
4729
4730     local ($ltxform);
4731     if (defined $configure_vars{'LIBTOOL'})
4732     {
4733         # Transform '@LIBTOOL ...@' to '$(LIBTOOL) ...'
4734         $ltxform = 's/\@LIBTOOL([^\@]*)\@/\$(LIBTOOL) $1/;';
4735     }
4736     else
4737     {
4738         # Delete '@LIBTOOL ...@'
4739         $ltxform = 's/\@LIBTOOL([^\@]*)\@//;';
4740     }
4741
4742     local ($cygxform);
4743     if (! $seen_cygwin32)
4744     {
4745         $cygxform = 's/\@EXEEXT\@//g; s/^NOTCYGWIN//; s/^CYGWIN.*$//;';
4746     }
4747     else
4748     {
4749         $cygxform = 's/\@EXEEXT\@/\$(EXEEXT)/g; s/^CYGWIN//; s/^NOTCYGWIN.*$//;';
4750     }
4751
4752     while (@args)
4753     {
4754         if ($args[0] eq '-clean')
4755         {
4756             $do_clean = 1;
4757         }
4758         elsif ($args[0] !~ /^-/)
4759         {
4760             last;
4761         }
4762         shift (@args);
4763     }
4764     local ($file, $primary, @prefixes) = @args;
4765
4766     local (@used) = ();
4767     local (@result) = ();
4768
4769     # Now that configure substitutions are allowed in where_HOW
4770     # variables, it is an error to actually define the primary.
4771     &am_line_error ($primary, "\`$primary' is an anachronism")
4772         if &variable_defined ($primary);
4773
4774
4775     # Look for misspellings.  It is an error to have a variable ending
4776     # in a "reserved" suffix whose prefix is unknown, eg
4777     # "bni_PROGRAMS".  However, unusual prefixes are allowed if a
4778     # variable of the same name (with "dir" appended) exists.  For
4779     # instance, if the variable "zardir" is defined, then
4780     # "zar_PROGRAMS" becomes valid.  This is to provide a little extra
4781     # flexibility in those cases which need it.  Perhaps it should be
4782     # disallowed in the Gnits case?  The problem is, sometimes it is
4783     # useful to put things in a subdir of eg pkgdatadir, perhaps even
4784     # for Gnitsoids.
4785     local (%valid) = &am_primary_prefixes ($primary, @prefixes);
4786
4787     # If a primary includes a configure substitution, then the EXTRA_
4788     # form is required.  Otherwise we can't properly do our job.
4789     local ($require_extra);
4790     local ($warned_about_extra) = 0;
4791
4792     local ($clean_file) = $file . '-clean';
4793     local ($one_name);
4794     local ($X);
4795     foreach $X (sort keys %valid)
4796     {
4797         $one_name = $X . '_' . $primary;
4798         if (&variable_defined ($one_name))
4799         {
4800             # Append actual contents of where_PRIMARY variable to
4801             # result.
4802             local ($rcurs);
4803             foreach $rcurs (&variable_value_as_list ($one_name))
4804             {
4805                 # Skip configure substitutions.  Possibly bogus.
4806                 if ($rcurs =~ /^\@.*\@$/)
4807                 {
4808                     if ($X eq 'EXTRA')
4809                     {
4810                         if (! $warned_about_extra)
4811                         {
4812                             $warned_about_extra = 1;
4813                             &am_line_error ($one_name,
4814                                             "\`$one_name' contains configure substitution, but shouldn't");
4815                         }
4816                     }
4817                     # Check here to make sure variables defined in
4818                     # configure.in do not imply that EXTRA_PRIMARY
4819                     # must be defined.
4820                     elsif (! defined $configure_vars{$one_name})
4821                     {
4822                         $require_extra = $one_name;
4823                     }
4824                     next;
4825                 }
4826                 push (@result, $rcurs);
4827             }
4828
4829             # "EXTRA" shouldn't be used when generating clean targets,
4830             # @all, or install targets.
4831             next if $X eq 'EXTRA';
4832
4833             if ($do_clean)
4834             {
4835                 $output_rules .=
4836                     &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go;'
4837                                                    . $cygxform,
4838                                                    $clean_file);
4839
4840                 push (@clean, $X . $primary);
4841                 &push_phony_cleaners ($X . $primary);
4842             }
4843
4844             if ($X eq 'check')
4845             {
4846                 push (@check, '$(' . $one_name . ')');
4847             }
4848             else
4849             {
4850                 push (@used, '$(' . $one_name . ')');
4851             }
4852             if ($X eq 'noinst' || $X eq 'check')
4853             {
4854                 # Objects which don't get installed by default.
4855                 next;
4856             }
4857
4858             $output_rules .=
4859                 &file_contents_with_transform ('s/\@DIR\@/' . $X . '/g;'
4860                                                . $ltxform . $cygxform,
4861                                                $file);
4862
4863             push (@uninstall, 'uninstall-' . $X . $primary);
4864             push (@phony, 'uninstall-' . $X . $primary);
4865             push (@installdirs, '$(' . $X . 'dir)');
4866             if (defined $exec_dir_p{$X} ? $exec_dir_p{$X} : ($X =~ /exec/))
4867             {
4868                 push (@install_exec, 'install-' . $X . $primary);
4869                 push (@phony, 'install-' . $X . $primary);
4870             }
4871             else
4872             {
4873                 push (@install_data, 'install-' . $X . $primary);
4874                 push (@phony, 'install-' . $X . $primary);
4875             }
4876         }
4877     }
4878
4879     if (@used)
4880     {
4881         # Define it.
4882         &define_pretty_variable ($primary, @used);
4883         $output_vars .= "\n";
4884     }
4885
4886     if ($require_extra && ! &variable_defined ('EXTRA_' . $primary))
4887     {
4888         &am_line_error ($require_extra,
4889                         "\`$require_extra' contains configure substitution, but \`EXTRA_$primary' not defined");
4890     }
4891
4892     # Push here because PRIMARY might be configure time determined.
4893     push (@all, '$(' . $primary . ')')
4894         if @used;
4895
4896     return (@result);
4897 }
4898
4899
4900 ################################################################
4901
4902 # This variable is local to the "require file" set of functions.
4903 @require_file_paths = ();
4904
4905 # Verify that the file must exist in the current directory.  Usage:
4906 # require_file (isconfigure, line_number, strictness, file) strictness
4907 # is the strictness level at which this file becomes required.  Must
4908 # set require_file_paths before calling this function.
4909 # require_file_paths is set to hold a single directory (the one in
4910 # which the first file was found) before return.
4911 sub require_file_internal
4912 {
4913     local ($is_configure, $line, $mystrict, @files) = @_;
4914     local ($file, $fullfile);
4915     local ($found_it, $errfile, $errdir);
4916     local ($save_dir);
4917
4918     foreach $file (@files)
4919     {
4920         $found_it = 0;
4921         foreach $dir (@require_file_paths)
4922         {
4923             if ($dir eq '.')
4924             {
4925                 $fullfile = $relative_dir . "/" . $file;
4926                 $errdir = $relative_dir unless $errdir;
4927             }
4928             else
4929             {
4930                 $fullfile = $dir . "/" . $file;
4931                 $errdir = $dir unless $errdir;
4932             }
4933
4934             # Use different name for "error filename".  Otherwise on
4935             # an error the bad file will be reported as eg
4936             # `../../install-sh' when using the default
4937             # config_aux_path.
4938             $errfile = $errdir . '/' . $file;
4939
4940             if (-f $fullfile)
4941             {
4942                 $found_it = 1;
4943                 # FIXME: Once again, special-case `.'.
4944                 &push_dist_common ($file)
4945                     if $dir eq $relative_dir || $dir eq '.';
4946                 $save_dir = $dir;
4947                 last;
4948             }
4949         }
4950
4951         if ($found_it)
4952         {
4953             # Prune the path list.
4954             @require_file_paths = $save_dir;
4955         }
4956         else
4957         {
4958             if ($strictness >= $mystrict)
4959             {
4960                 local ($trailer) = '';
4961                 local ($suppress) = 0;
4962
4963                 # Only install missing files according to our desired
4964                 # strictness level.
4965                 if ($add_missing)
4966                 {
4967                     $trailer = "; installing";
4968                     $suppress = 1;
4969
4970                     # Maybe run libtoolize.
4971                     if ($seen_libtool
4972                         && grep ($_ eq $file, @libtoolize_files)
4973                         && system ('libtoolize', '--automake'))
4974                     {
4975                         $suppress = 0;
4976                         $trailer .= "; cannot run \`libtoolize': $!";
4977                     }
4978                     elsif (-f ($am_dir . '/' . $file))
4979                     {
4980                         # Install the missing file.  Symlink if we
4981                         # can, copy if we must.  Note: delete the file
4982                         # first, in case it is a dangling symlink.
4983                         unlink ($errfile);
4984                         if ($symlink_exists)
4985                         {
4986                             if (! symlink ($am_dir . '/' . $file, $errfile))
4987                             {
4988                                 $suppress = 0;
4989                                 $trailer .= "; error while making link: $!\n";
4990                             }
4991                         }
4992                         elsif (! system ('cp', $am_dir . '/' . $file, $errfile))
4993                         {
4994                             $suppress = 0;
4995                             $trailer .= "\n    error while making link\n";
4996                         }
4997                     }
4998                 }
4999
5000                 local ($save) = $exit_status;
5001                 if ($is_configure)
5002                 {
5003                     # FIXME: allow actual file to be specified.
5004                     &am_conf_line_error
5005                         ('configure.in', $line,
5006                          "required file \"$errfile\" not found$trailer");
5007                 }
5008                 else
5009                 {
5010                     &am_line_error
5011                         ($line,
5012                          "required file \"$errfile\" not found$trailer");
5013                 }
5014                 $exit_status = $save if $suppress;
5015             }
5016         }
5017     }
5018 }
5019
5020 # Like require_file_with_line, but error messages refer to
5021 # configure.in, not the current Makefile.am.
5022 sub require_file_with_conf_line
5023 {
5024     @require_file_paths = '.';
5025     &require_file_internal (1, @_);
5026 }
5027
5028 sub require_file_with_line
5029 {
5030     @require_file_paths = '.';
5031     &require_file_internal (0, @_);
5032 }
5033
5034 sub require_file
5035 {
5036     @require_file_paths = '.';
5037     &require_file_internal (0, '', @_);
5038 }
5039
5040 # Require a file that is also required by Autoconf.  Looks in
5041 # configuration path, as specified by AC_CONFIG_AUX_DIR.
5042 sub require_config_file
5043 {
5044     @require_file_paths = @config_aux_path;
5045     &require_file_internal (1, '', @_);
5046     local ($dir) = $require_file_paths[0];
5047     @config_aux_path = @require_file_paths;
5048     if ($dir eq '.')
5049     {
5050         $config_aux_dir = '.';
5051     }
5052     else
5053     {
5054         $config_aux_dir = '$(top_srcdir)/' . $dir;
5055     }
5056 }
5057
5058 # Assumes that the line number is in Makefile.am.
5059 sub require_conf_file_with_line
5060 {
5061     @require_file_paths = @config_aux_path;
5062     &require_file_internal (0, @_);
5063     local ($dir) = $require_file_paths[0];
5064     @config_aux_path = @require_file_paths;
5065     if ($dir eq '.')
5066     {
5067         $config_aux_dir = '.';
5068     }
5069     else
5070     {
5071         $config_aux_dir = '$(top_srcdir)/' . $dir;
5072     }
5073 }
5074
5075 # Assumes that the line number is in Makefile.am.
5076 sub require_conf_file_with_conf_line
5077 {
5078     @require_file_paths = @config_aux_path;
5079     &require_file_internal (1, @_);
5080     local ($dir) = $require_file_paths[0];
5081     @config_aux_path = @require_file_paths;
5082     if ($dir eq '.')
5083     {
5084         $config_aux_dir = '.';
5085     }
5086     else
5087     {
5088         $config_aux_dir = '$(top_srcdir)/' . $dir;
5089     }
5090 }
5091
5092 ################################################################
5093
5094 # Push a list of files onto dist_common.
5095 sub push_dist_common
5096 {
5097     local (@files) = @_;
5098     local ($file);
5099
5100     foreach $file (@files)
5101     {
5102         $dist_common{$file} = 1;
5103     }
5104 }
5105
5106 # Push a list of clean targets onto phony.
5107 sub push_phony_cleaners
5108 {
5109     local ($base) = @_;
5110     local ($target);
5111     foreach $target ('mostly', 'dist', '', 'maintainer-')
5112     {
5113         push (@phony, $target . 'clean-' . $base);
5114     }
5115 }
5116
5117 # Set strictness.
5118 sub set_strictness
5119 {
5120     $strictness_name = $_[0];
5121     if ($strictness_name eq 'gnu')
5122     {
5123         $strictness = $GNU;
5124     }
5125     elsif ($strictness_name eq 'gnits')
5126     {
5127         $strictness = $GNITS;
5128     }
5129     elsif ($strictness_name eq 'foreign')
5130     {
5131         $strictness = $FOREIGN;
5132     }
5133     else
5134     {
5135         die "automake: level \`$strictness_name' not recognized\n";
5136     }
5137 }
5138
5139
5140 ################################################################
5141
5142 # Return directory name of file.
5143 sub dirname
5144 {
5145     local ($file) = @_;
5146     local ($sub);
5147
5148     ($sub = $file) =~ s,/+[^/]+$,,g;
5149     $sub = '.' if $sub eq $file;
5150     return $sub;
5151 }
5152
5153 # Return file name of a file.
5154 sub basename
5155 {
5156     local ($file) = @_;
5157     local ($sub);
5158
5159     ($sub = $file) =~s,^.*/+,,g;
5160     return $sub;
5161 }
5162
5163 # Touch a file.
5164 sub touch
5165 {
5166     local ($file) = @_;
5167
5168     open (TOUCH, ">> $file");
5169     close (TOUCH);
5170 }
5171
5172 # Glob something.  Do this to avoid indentation screwups everywhere we
5173 # want to glob.  Gross!
5174 sub my_glob
5175 {
5176     local ($pat) = @_;
5177     return <${pat}>;
5178 }
5179
5180 ################################################################
5181
5182 # Print an error message and set exit status.
5183 sub am_error
5184 {
5185     warn "automake: ${am_file}.am: ", join (' ', @_), "\n";
5186     $exit_status = 1;
5187 }
5188
5189 sub am_line_error
5190 {
5191     local ($symbol, @args) = @_;
5192
5193     if ($symbol && "$symbol" ne '-1')
5194     {
5195         # If SYMBOL not already a line number, look it up in Makefile.am.
5196         if ($symbol =~ /^\d+$/)
5197         {
5198             $symbol .= ': ';
5199         }
5200         elsif (defined $content_lines{$symbol})
5201         {
5202             $symbol = $content_lines{$symbol} . ': ';
5203         }
5204         else
5205         {
5206             # A single space, to provide nice separation.
5207             $symbol = ' ';
5208         }
5209         warn "${am_file}.am:", $symbol, join (' ', @args), "\n";
5210         $exit_status = 1;
5211     }
5212     else
5213     {
5214         &am_error (@args);
5215     }
5216 }
5217
5218 # Like am_error, but while scanning configure.in.
5219 sub am_conf_error
5220 {
5221     # FIXME: can run in subdirs.
5222     warn "automake: configure.in: ", join (' ', @_), "\n";
5223     $exit_status = 1;
5224 }
5225
5226 # Error message with line number referring to configure.in.
5227 sub am_conf_line_error
5228 {
5229     local ($file, $line, @args) = @_;
5230
5231     if ($line)
5232     {
5233         warn "$file: $line: ", join (' ', @args), "\n";
5234         $exit_status = 1;
5235     }
5236     else
5237     {
5238         &am_conf_error (@args);
5239     }
5240 }
5241
5242 # Tell user where our aclocal.m4 is, but only once.
5243 sub keyed_aclocal_warning
5244 {
5245     local ($key) = @_;
5246     warn "automake: macro \`$key' can be generated by \`aclocal'\n";
5247 }
5248
5249 # Print usage information.
5250 sub usage
5251 {
5252     print "Usage: automake [OPTION] ... [Makefile]...\n";
5253     print $USAGE;
5254     print "\nFiles which are automatically distributed, if found:\n";
5255     $~ = "USAGE_FORMAT";
5256     local (@lcomm) = sort ((@common_files, @common_sometimes));
5257     local ($one, $two, $three, $four);
5258     while (@lcomm > 0)
5259     {
5260         $one = shift @lcomm;
5261         $two = @lcomm ? shift @lcomm : '';
5262         $three = @lcomm ? shift @lcomm : '';
5263         $four = @lcomm ? shift @lcomm : '';
5264         write;
5265     }
5266
5267     print "\nReport bugs to <automake-bugs\@gnu.ai.mit.edu>\n";
5268
5269     exit 0;
5270 }
5271
5272 format USAGE_FORMAT =
5273   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<
5274   $one,               $two,               $three,             $four
5275 .