10a49b68d14a737b8573146a69404aa15f2d7f04
[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@drip.colorado.edu>.
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
37 # String constants.
38 $IGNORE_PATTERN = "^##([^#].*)?\$";
39 $WHITE_PATTERN = "^[ \t]*\$";
40 $COMMENT_PATTERN = "^#";
41 $RULE_PATTERN = "^([\$a-zA-Z_.][-.a-zA-Z0-9_(){}/]*) *:([^=].*|)\$";
42 $MACRO_PATTERN = "^([A-Za-z][A-Za-z0-9_]*)[ \t]*:?=[ \t]*(.*)\$";
43 $BOGUS_MACRO_PATTERN = "^([^ \t]*)[ \t]*:?=[ \t]*(.*)\$";
44 $GNITS_VERSION_PATTERN = "[0-9]+\\.[0-9]+([a-z]|\\.[0-9]+)?";
45
46 # Constants to define the "strictness" level.
47 $FOREIGN = 0;
48 $GNU = 1;
49 $GNITS = 2;
50
51 \f
52
53 # Variables global to entire run.
54
55 # TRUE if we should always generate Makefile.in.
56 $force_generation = 1;
57
58 # Strictness level as set on command line.
59 $default_strictness = $GNU;
60
61 # Name of strictness level, as set on command line.
62 $default_strictness_name = 'gnu';
63
64 # This is TRUE if GNU make specific automatic dependency generation
65 # code should be included in generated Makefile.in.
66 $cmdline_use_dependencies = 1;
67
68 # TRUE if in verbose mode.
69 $verbose = 0;
70
71 # This holds our (eventual) exit status.  We don't actually exit until
72 # we have processed all input files.
73 $exit_status = 0;
74
75 # From the Perl manual.
76 $symlink_exists = (eval 'symlink ("", "");', $@ eq '');
77
78 # TRUE if missing standard files should be installed.
79 $add_missing = 0;
80
81 # Files found by scanning configure.in for LIBOBJS.
82 %libsources = ();
83
84 # True if AM_C_PROTOTYPES appears in configure.in.
85 $am_c_prototypes = 0;
86
87 # Names used in AC_CONFIG_HEADER call.  $config_name is the actual
88 # (first) argument.  $config_header is the '.in' file.  Ordinarily the
89 # second is derived from the first, but they can be different if the
90 # weird "NAME:FILE" syntax is used.
91 $config_name = '';
92 $config_header = '';
93 # Line number at which AC_CONFIG_HEADER appears in configure.in.
94 $config_header_line = 0;
95
96 # Directory where output files go.  Actually, output files are
97 # relative to this directory.
98 $output_directory = '.';
99
100 # Relative location of top build directory.
101 $top_builddir = '';
102
103 # Absolute location of top build directory.
104 $build_directory = '';
105
106 # Name of srcdir as given in build directory's Makefile.  For
107 # dependencies only.
108 $srcdir_name = '';
109
110 # List of Makefile.am's to process.
111 @input_files = ();
112
113 # List of files in AC_OUTPUT without Makefile.am.
114 @other_input_files = ();
115 # Line number at which AC_OUTPUT seen.
116 $ac_output_line = 0;
117
118 # List of directories to search for configure-required files.  This
119 # can be set by AC_CONFIG_AUX_DIR.
120 @config_aux_path = ('.', '..', '../..');
121 $config_aux_dir = '';
122
123 # Whether AC_PROG_MAKE_SET has been seen in configure.in.
124 $seen_make_set = 0;
125
126 # Whether ud_GNU_GETTEXT has been seen in configure.in.
127 $seen_gettext = 0;
128 # Line number at which ud_GNU_GETTEXT seen.
129 $ac_gettext_line = 0;
130
131 # Whether ALL_LINGUAS has been seen.
132 $seen_linguas = '';
133 # The actual text.
134 $all_linguas = '';
135 # Line number at which it appears.
136 $all_linguas_line = 0;
137
138 # 1 if AC_PROG_INSTALL seen, 2 if AM_PROG_INSTALL seen.
139 $seen_prog_install = 0;
140
141 # 1 if any scripts installed, 0 otherwise.
142 $scripts_installed = 0;
143
144 # Whether AC_PATH_XTRA has been seen in configure.in.
145 $seen_path_xtra = 0;
146
147 # Whether YACC variable has been seen in configure.in.
148 $seen_prog_yacc = 0;
149
150 # Two variables to control lex use.  One is for AC_DECL_YYTEXT and the
151 # other is for AC_PROG_LEX.
152 $seen_decl_yytext = 0;
153 $seen_prog_lex = 0;
154
155 # TRUE if we've seen AC_PROG_CC.
156 $seen_prog_cc = 0;
157
158 # TRUE if we've seen AC_PROG_CXX.
159 $seen_prog_cxx = 0;
160
161 # TRUE if we've seen AC_CANONICAL_(HOST|SYSTEM).  The presence of
162 # AC_CHECK_TOOL also sets this.
163 $seen_canonical = 0;
164
165 # TRUE if we've seen AC_PROG_RANLIB.
166 $seen_ranlib = 0;
167
168 # TRUE if we've seen AC_ARG_PROGRAM.
169 $seen_arg_prog = 0;
170
171 # TRUE if we've seen AM_PROG_LIBTOOL.
172 $seen_libtool = 0;
173 $libtool_line = 0;
174
175 # TRUE if we've seen AM_MAINTAINER_MODE.
176 $seen_maint_mode = 0;
177
178 # TRUE if we've seen PACKAGE and VERSION.
179 $seen_package = 0;
180 $seen_version = 0;
181
182 # Actual version we've seen.
183 $package_version = '';
184
185 # Line number where we saw version definition.
186 $package_version_line = 0;
187
188 # TRUE if we've seen AM_PATH_LISPDIR.
189 $seen_lispdir = 0;
190
191
192 # Charsets used by maintainer and in distribution.  MAINT_CHARSET is
193 # handled in a funny way: if seen in the top-level Makefile.am, it is
194 # used for every directory which does not specify a different value.
195 # The rationale here is that some directories (eg gettext) might be
196 # distributions of other packages, and thus require their own charset
197 # info.  However, the DIST_CHARSET must be the same for the entire
198 # package; it can only be set at top-level.
199 # FIXME: this yields bugs when rebuilding.  What to do?  Always
200 # read (and sometimes discard) top-level Makefile.am?
201 $maint_charset = '';
202 $dist_charset = 'utf8';         # recode doesn't support this yet.
203
204 # Name of input file ("Makefile.in") and output file ("Makefile.am").
205 # These have no directory components.
206 $am_file_name = '';
207 $in_file_name = '';
208
209 \f
210
211 &initialize_global_constants;
212
213 # Parse command line.
214 &parse_arguments (@ARGV);
215
216 # Do configure.in scan only once.
217 &scan_configure;
218
219 die "automake: no \`Makefile.am' found or specified\n"
220     if ! @input_files;
221
222 # Now do all the work on each file.
223 foreach $am_file (@input_files)
224 {
225     # FIXME: should support the AC_OUTPUT ":" syntax here.
226     if (! -f ($am_file . '.am'))
227     {
228         &am_error ('no such file');
229     }
230     else
231     {
232         &generate_makefile ($am_file);
233     }
234 }
235
236 if ($seen_prog_install <= $scripts_installed)
237 {
238     &am_conf_error (($scripts_installed ? 'AM_PROG_INSTALL' : 'AC_PROG_INSTALL')
239                     . " must be used in configure.in");
240     &keyed_aclocal_warning ('AM_PROG_INSTALL')
241         if $scripts_installed;
242 }
243
244 exit $exit_status;
245
246
247 ################################################################
248
249 # Parse command line.
250 sub parse_arguments
251 {
252     local (@arglist) = @_;
253
254     # Start off as gnu.
255     &set_strictness ('gnu');
256
257     while (@arglist)
258     {
259         if ($arglist[0] eq "--version")
260         {
261             print "automake - GNU $PACKAGE $VERSION\n";
262             exit 0;
263         }
264         elsif ($arglist[0] eq "--help")
265         {
266             &usage;
267         }
268         elsif ($arglist[0] =~ /^--amdir=(.+)$/)
269         {
270             $am_dir = $1;
271         }
272         elsif ($arglist[0] eq '--amdir')
273         {
274             &require_argument (@arglist);
275             shift (@arglist);
276             $am_dir = $arglist[0];
277         }
278         elsif ($arglist[0] =~ /^--build-dir=(.+)$/)
279         {
280             # Must end in /.
281             $build_directory = $1 . '/';
282         }
283         elsif ($arglist[0] eq '--build-dir')
284         {
285             &require_argument (@arglist);
286             shift (@arglist);
287             # Must end in /.
288             $build_directory = $arglist[0] . '/';
289         }
290         elsif ($arglist[0] =~ /^--srcdir-name=(.+)$/)
291         {
292             $srcdir_name = $1;
293         }
294         elsif ($arglist[0] eq '--srcdir-name')
295         {
296             &require_argument (@arglist);
297             shift (@arglist);
298             $srcdir_name = $arglist[0];
299         }
300         elsif ($arglist[0] =~ /^--strictness=(.+)$/)
301         {
302             &set_strictness ($1);
303         }
304         elsif ($arglist[0] eq '--gnu')
305         {
306             &set_strictness ('gnu');
307         }
308         elsif ($arglist[0] eq '--gnits')
309         {
310             &set_strictness ('gnits');
311         }
312         elsif ($arglist[0] eq '--foreign')
313         {
314             &set_strictness ('foreign');
315         }
316         elsif ($arglist[0] eq '--strictness' || $arglist[0] eq '-s')
317         {
318             &require_argument (@arglist);
319             shift (@arglist);
320             &set_strictness ($arglist[0]);
321         }
322         elsif ($arglist[0] eq '--include-deps' || $arglist[0] eq '-i')
323         {
324             $cmdline_use_dependencies = 0;
325         }
326         elsif ($arglist[0] eq '--no-force')
327         {
328             $force_generation = 0;
329         }
330         elsif ($arglist[0] =~ /^--output-dir=(.*)$/)
331         {
332             # Set output directory.
333             $output_directory = $1;
334         }
335         elsif ($arglist[0] eq '--output-dir' || $arglist[0] eq '-o')
336         {
337             &require_argument (@arglist);
338             shift (@arglist);
339             $output_directory = $arglist[0];
340         }
341         elsif ($arglist[0] eq '--add-missing' || $arglist[0] eq '-a')
342         {
343             $add_missing = 1;
344         }
345         elsif ($arglist[0] eq '--verbose' || $arglist[0] eq '-v')
346         {
347             $verbose = 1;
348         }
349         elsif ($arglist[0] eq '--')
350         {
351             # Stop option processing.
352             shift (@arglist);
353             push (@input_files, @arglist);
354             last;
355         }
356         elsif ($arglist[0] =~ /^-/)
357         {
358             die "automake: unrecognized option -- \`$arglist[0]'\n";
359         }
360         else
361         {
362             push (@input_files, $arglist[0]);
363         }
364
365         shift (@arglist);
366     }
367
368     # Take global strictness from whatever we currently have set.
369     $default_strictness = $strictness;
370     $default_strictness_name = $strictness_name;
371 }
372
373 # Ensure argument exists, or die.
374 sub require_argument
375 {
376     local ($arg, @arglist) = @_;
377     die "automake: no argument given for option \`$arg'\n"
378         if ! @arglist;
379 }
380
381 ################################################################
382
383 # Generate a Makefile.in given the name of the corresponding Makefile.
384 sub generate_makefile
385 {
386     local ($makefile) = @_;
387
388     ($am_file_name = $makefile) =~ s/^.*\///;
389     $in_file_name = $am_file_name . '.in';
390     $am_file_name .= '.am';
391
392     &initialize_per_input;
393     $relative_dir = &dirname ($makefile);
394
395     # At the toplevel directory, we might need config.guess, config.sub
396     # or libtool.
397     if ($relative_dir eq '.')
398     {
399         # libtool requires some files.
400         &require_conf_file_with_conf_line ($libtool_line, $FOREIGN,
401                                            'config.sub', 'config.guess',
402                                            'libtool') if $seen_libtool;
403
404         # AC_CANONICAL_HOST and AC_CANONICAL_SYSTEM need config.guess and
405         # config.sub.
406         &require_config_file ($FOREIGN, 'config.guess', 'config.sub')
407             if $seen_canonical;
408     }
409
410     # We still need Makefile.in here, because sometimes the `dist'
411     # target doesn't re-run automake.
412     &push_dist_common ($in_file_name, $am_file_name);
413     push (@sources, '$(SOURCES)')
414         if &variable_defined ('SOURCES');
415     push (@objects, '$(OBJECTS)')
416         if &variable_defined ('OBJECTS');
417
418     # This is always the default target.  This gives us freedom to do
419     # things in whatever order is convenient.
420     $output_rules .= "default: all\n\n";
421     push (@phony, 'default');
422
423     &read_am_file ($makefile . '.am');
424     if (&handle_options)
425     {
426         # Fatal error.  Just return, so we can continue with next file.
427         return;
428     }
429
430     # Check first, because we might modify some state.
431     &check_gnu_standards;
432     &check_gnits_standards;
433
434     &handle_configure;
435     &handle_gettext;
436     &handle_libraries;
437     &handle_programs;
438     &check_typos;
439     &handle_scripts;
440
441     # This must be run after all the sources are scanned.
442     &handle_yacc_lex_cxx;
443
444     # Re-init SOURCES and OBJECTS.  FIXME: other code shouldn't depend
445     # on this (but currently does).
446     $contents{'SOURCES'} = join (' ', @sources);
447     $contents{'OBJECTS'} = join (' ', @objects);
448
449     &handle_texinfo;
450     &handle_emacs_lisp;
451     &handle_man_pages;
452     &handle_data;
453     &handle_headers;
454     &handle_subdirs;
455     &handle_tags;
456     &handle_dist;
457     &handle_dependencies;
458     &handle_tests;
459     &handle_footer;
460     &handle_merge_targets;
461     &handle_installdirs;
462     &handle_clean;
463     &handle_phony;
464
465     if (! -d ($output_directory . '/' . $relative_dir))
466     {
467         mkdir ($output_directory . '/' . $relative_dir, 0755);
468     }
469
470     local ($out_file) = $output_directory . '/' . $makefile . ".in";
471     if (! $force_generation && -e $out_file)
472     {
473         local ($am_time) = (stat ($makefile . '.am'))[9];
474         local ($in_time) = (stat ($out_file))[9];
475         # FIXME: how to do unsigned comparison?
476         if ($am_time < $in_time)
477         {
478             # No need to update.
479             return;
480         }
481     }
482
483     if (! open (GM_FILE, "> " . $out_file))
484     {
485         warn "automake: ${am_file}.in: cannot open: $!\n";
486         $exit_status = 1;
487         return;
488     }
489     print "automake: creating ", $makefile, ".in\n" if $verbose;
490
491     print GM_FILE $output_vars;
492     print GM_FILE $output_rules;
493     print GM_FILE $output_trailer;
494
495     close (GM_FILE);
496 }
497
498 ################################################################
499
500 # Handle AUTOMAKE_OPTIONS variable.  Return 1 on error, 0 otherwise.
501 sub handle_options
502 {
503     return 0 if ! &variable_defined ('AUTOMAKE_OPTIONS');
504
505     foreach (&variable_value_as_list ('AUTOMAKE_OPTIONS'))
506     {
507         last if /^#/;
508
509         $options{$_} = 1;
510         if ($_ eq 'gnits' || $_ eq 'gnu' || $_ eq 'foreign')
511         {
512             &set_strictness ($_);
513         }
514         elsif (/ansi2knr/)
515         {
516             # An option like "../lib/ansi2knr" is allowed.  With no
517             # path prefix, we assume the required programs are in this
518             # directory.  We save the actual option for later.
519             $options{'ansi2knr'} = $_;
520         }
521         elsif ($_ eq 'no-installman' || $_ eq 'no-installinfo'
522                || $_ eq 'dist-shar' || $_ eq 'dist-zip'
523                || $_ eq 'dist-tarZ' || $_ eq 'dejagnu')
524         {
525             # Explicitly recognize these.
526         }
527         elsif ($_ eq 'no-dependencies')
528         {
529             $use_dependencies = 0;
530         }
531         elsif (/([0-9]+)\.([0-9]+)/)
532         {
533             # Got a version number.  Note that alpha releases count as
534             # the next higher release.  Note also that we assume there
535             # will be a maximum of 100 minor releases for any given
536             # major release.
537             local ($rmajor, $rminor) = ($1, $2);
538             if ($VERSION !~ /([0-9]+)\.([0-9]+)([a-z])?/)
539             {
540                 print STDERR
541                     "automake: programming error: version is incorrect\n";
542                 exit 1;
543             }
544             local ($tmajor, $tminor) = ($1, $2);
545             # Handle alpha versions.
546             ++$tminor if defined $3;
547
548             if ($rmajor > $tmajor || ($rmajor == $tmajor && $rminor > $tminor))
549             {
550                 &am_line_error ('AUTOMAKE_OPTIONS',
551                                 "require version $_, only have $VERSION");
552                 return 1;
553             }
554         }
555         else
556         {
557             &am_line_error ('AUTOMAKE_OPTIONS',
558                             'option ', $_, 'not recognized');
559         }
560     }
561
562     return 0;
563 }
564
565 # Return object extension.  Just once, put some code into the output.
566 sub get_object_extension
567 {
568     if (! $dir_holds_sources)
569     {
570         # Boilerplate.
571         local ($xform) = '';
572         if (&variable_defined ('CONFIG_HEADER'))
573         {
574             ($xform = &dirname ($contents{'CONFIG_HEADER'}))
575                 =~ s/(\W)/\\$1/g;
576             $xform = '-I' . $xform;
577         }
578         $xform = 's/\@CONFIG_INCLUDE_SPEC\@/' . $xform . '/go';
579         $output_vars .= &file_contents_with_transform ($xform,
580                                                        'compile-vars');
581         $output_rules .= &file_contents ('compile');
582         &push_phony_cleaners ('compile');
583
584         # If using X, include some extra variable definitions.  NOTE
585         # we don't want to force these into CFLAGS or anything,
586         # because not all programs will necessarily use X.
587         if ($seen_path_xtra)
588         {
589             local ($var);
590             foreach $var ('X_CFLAGS', 'X_LIBS', 'X_EXTRA_LIBS', 'X_PRE_LIBS')
591             {
592                 &define_variable ($var, "\@$var\@");
593             }
594         }
595
596         # Check for automatic de-ANSI-fication.
597         $dir_holds_sources = '.o';
598         push (@suffixes, '.c', '.o');
599         push (@clean, 'compile');
600
601         if (defined $options{'ansi2knr'})
602         {
603             if (! $am_c_prototypes)
604             {
605                 &am_line_error ('AUTOMAKE_OPTIONS',
606                                 "option \`ansi2knr' in use but \`AM_C_PROTOTYPES' not in configure.in");
607                 &keyed_aclocal_warning ('AM_C_PROTOTYPES');
608                 # Only give this error once.
609                 $am_c_prototypes = 1;
610             }
611
612             $dir_holds_sources = '$o';
613             push (@suffixes, '._c', '._o');
614
615             # Only require ansi2knr files if they should appear in
616             # this directory.
617             if ($options{'ansi2knr'} eq 'ansi2knr')
618             {
619                 &require_file_with_line ('AUTOMAKE_OPTIONS', $FOREIGN,
620                                          'ansi2knr.c', 'ansi2knr.1');
621                 $output_rules .= &file_contents ('kr-extra');
622                 push (@clean, 'krextra');
623                 &push_phony_cleaners ('krextra');
624             }
625
626             &define_variable ('o', ".\@U\@o");
627
628             # Make sure ansi2knr can be found: if no path specified,
629             # specify "./".
630             local ($apath) = $options{'ansi2knr'};
631             $apath = './' . $apath
632                 unless $apath =~ /\//;
633             &define_variable ("ANSI2KNR", $apath);
634
635             $output_rules .= &file_contents ('compile-kr');
636             $output_rules .= &file_contents ('clean-kr');
637
638             push (@clean, 'kr');
639             &push_phony_cleaners ('kr');
640         }
641     }
642     return $dir_holds_sources;
643 }
644
645 # Handle yacc and lex.
646 sub handle_yacc_lex_cxx
647 {
648     #
649     # First do yacc and lex.
650     #
651
652     local ($yacc_count) = scalar (keys %yacc_sources);
653     local ($lex_count) = scalar (keys %lex_sources);
654     if ($yacc_count)
655     {
656         push (@suffixes, '.y');
657         &define_variable ('YACC', "\@YACC\@");
658         $output_rules .= ".y.c:\n\t";
659         if ($yacc_count > 1)
660         {
661             $output_rules .= '$(INTERLOCK) =yacclockdir $(YLWRAP) y.tab.c $@ $(YACC) $(YFLAGS) $<';
662         }
663         else
664         {
665             $output_rules .= '$(YACC) $(YFLAGS) $< && mv y.tab.c $@';
666         }
667         $output_rules .= "\n";
668
669         if (! $seen_prog_yacc)
670         {
671             # FIXME: should include a reference line.  FIXME: maybe
672             # directly reference AC_PROG_YACC somehow?
673             &am_error ("yacc source seen but \`YACC' not defined in \`configure.in'\n");
674         }
675     }
676     if ($lex_count)
677     {
678         push (@suffixes, '.l');
679         &define_variable ("LEX", "\@LEX\@");
680         &define_variable ("LEX_OUTPUT_ROOT", "\@LEX_OUTPUT_ROOT\@");
681         $output_rules .= ".l.c:\n\t";
682         if ($lex_count > 1)
683         {
684             $output_rules .= '$(INTERLOCK) =lexlockdir $(YLWRAP) $(LEX_OUTPUT_ROOT).c $@ $(LEX) $(LFLAGS) $<';
685         }
686         else
687         {
688             $output_rules .= '$(LEX) $(LFLAGS) $< && mv $(LEX_OUTPUT_ROOT).c $@';
689         }
690         $output_rules .= "\n";
691
692         if (! $seen_prog_lex)
693         {
694             &am_error ("lex source seen but \`AC_PROG_LEX' not in \`configure.in'\n");
695         }
696         if (! $seen_decl_yytext)
697         {
698             &am_error ("lex source seen but \`AC_DECL_YYTEXT' not in \`configure.in'\n");
699         }
700     }
701
702     if ($yacc_count > 1 || $lex_count > 1)
703     {
704         # If there is more than one distinct yacc (resp lex) source
705         # file in a given directory, then the `interlock' program is
706         # required to allow parallel builds to work correctly.  FIXME:
707         # for now, no line number.
708         &require_config_file ($FOREIGN, 'interlock', 'ylwrap');
709         &define_variable ('INTERLOCK', $config_aux_dir . "/interlock");
710         &define_variable ('YLWRAP', $config_aux_dir . "/ylwrap");
711     }
712
713     #
714     # Now handle C++.
715     #
716     local (@cxx_list) = keys %cxx_extensions;
717     local ($cxx_count) = scalar @cxx_list;
718     if ($cxx_count)
719     {
720         push (@suffixes, @cxx_list);
721
722         &define_variable ("CXX", "\@CXX\@");
723         &define_variable ("CXXFLAGS", "\@CXXFLAGS\@");
724         &define_variable ('CXXCOMPILE', '$(CXX) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS)');
725         &define_variable ('CXXLINK', '$(CXX) $(LDFLAGS) -o $@');
726
727         local ($ext);
728         foreach $ext (@cxx_list)
729         {
730             $output_rules .= ("$ext.o:\n"
731                               . "\t\$(CXXCOMPILE) -c \$<\n");
732         }
733
734         if (! $seen_prog_cxx)
735         {
736             &am_error ("C++ source seen but \`AC_PROG_CXX' not in \`configure.in'\n");
737         }
738     }
739
740     #
741     # Last, handle some C cleanup.
742     #
743     if ($seen_c_source)
744     {
745         &define_variable ('CC', "\@CC\@");
746         &define_variable ('CFLAGS', "\@CFLAGS\@");
747         &define_variable ('COMPILE',
748                           '$(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)');
749         &define_variable ('LINK', '$(CC) $(LDFLAGS) -o $@');
750     }
751 }
752
753 # Handle SOURCE->OBJECT transform for one program or library.
754 # Arguments are:
755 #   canonical (transformed) name of object to build
756 #   actual name of object to build
757 #   object extension (ie either `.o' or `$o'.
758 # Return result is name of linker variable that must be used.
759 # Empty return means just use `LINK'.
760 sub handle_source_transform
761 {
762     # one_file is canonical name.  unxformed is given name.  obj is
763     # object extension.
764     local ($one_file, $unxformed, $obj) = @_;
765     local ($objpat) = $obj;
766     $objpat =~ s/(\W)/\\$1/g;
767
768     local ($linker) = '';
769
770     if (&variable_defined ($one_file . "_OBJECTS"))
771     {
772         &am_line_error ($one_file . '_OBJECTS',
773                         $one_file . '_OBJECTS', 'should not be defined');
774         # No point in continuing.
775         return;
776     }
777
778     local (@files);
779     local ($prefix);
780     foreach $prefix ('', 'EXTRA_')
781     {
782         @files = ();
783         if (&variable_defined ($prefix . $one_file . "_SOURCES"))
784         {
785             push (@sources, '$(' . $prefix . $one_file . "_SOURCES)");
786             push (@objects, '$(' . $prefix . $one_file . "_OBJECTS)")
787                 unless $prefix eq 'EXTRA_';
788             @files = &variable_value_as_list ($prefix
789                                               . $one_file . "_SOURCES");
790         }
791         elsif ($prefix eq '')
792         {
793             &define_variable ($one_file . "_SOURCES", $unxformed . ".c");
794             push (@sources, $unxformed . '.c');
795             push (@objects, $unxformed . $obj);
796             push (@files, $unxformed . '.c');
797         }
798         else
799         {
800             &define_variable ("EXTRA_" . $one_file . "_SOURCES", '');
801         }
802
803         if (length (@files))
804         {
805             # Turn sources into objects.
806             local (@result) = ();
807             foreach (@files)
808             {
809                 # Skip header files, including C++-ish ones.  The list
810                 # of C++ header extensions comes from Emacs 19.32
811                 # etags.
812                 next if /\.[hH]$/;
813                 next if /\.hxx$/;
814                 next if /\.h\+\+$/;
815                 next if /\.hh$/;
816                 next if /\.hpp$/;
817                 # Skip things that look like configure substitutions.
818                 next if /^\@.*\@$/;
819
820                 # Include .c file for lex or yacc source in distribution.
821                 if (/^(.*)\.y$/)
822                 {
823                     # Yacc source.
824                     &push_dist_common ($1 . '.c');
825                     $yacc_sources{$_} = 1;
826                 }
827                 elsif (/^(.*)\.l$/)
828                 {
829                     # Lex source.
830                     &push_dist_common ($1 . '.c');
831                     $lex_sources{$_} = 1;
832                 }
833
834                 # Transform source files into .o files.  List of C++
835                 # extensions comes from Emacs 19.32 etags.
836                 if (s/\.c\+\+$/$obj/g
837                     || s/\.cc$/$obj/g
838                     || s/\.cpp$/$obj/g
839                     || s/\.cxx$/$obj/g
840                     || s/\.C$/$obj/g)
841                 {
842                     $cxx_extensions{$&} = 1;
843                     $linker = 'CXXLINK';
844                 }
845                 else
846                 {
847                     # FORTRAN support.  FIXME: not finished.
848                     s/\.f90$/$obj/g;
849                     s/\.for$/$obj/g;
850
851                     # .y is yacc.  .l is lex.  .f and .F is fortran.
852                     # .s is assembly.  .M is Objective-C++.  .m is
853                     # Objective-C.
854                     s/\.[cylfFsmM]$/$obj/g;
855
856                     # FIXME: of course, this should only happen for C
857                     # source.  The multi-language support must really
858                     # be cleaned up more globally.
859                     $seen_c_source = 1;
860                 }
861
862                 push (@result, $_)
863                     unless $prefix eq 'EXTRA_';
864
865                 # Transform .o or $o file into .P file (for automatic
866                 # dependency code).
867                 s/$objpat$/.P/g;
868                 $dep_files{'.deps/' . $_} = 1;
869             }
870
871             &define_pretty_variable ($one_file . "_OBJECTS", @result)
872                 unless $prefix eq 'EXTRA_';
873         }
874     }
875
876     if (&variable_defined ('CONFIG_HEADER'))
877     {
878         $output_rules .= ('$(' . $one_file . "_OBJECTS): "
879                           . $contents{'CONFIG_HEADER'} . "\n");
880     }
881
882     return $linker;
883 }
884
885 # Special-case @ALLOCA@ and @LIBOBJS@ in _LDADD or _LIBADD variables.
886 # Also, generate _DEPENDENCIES variable if appropriate.
887 # Arguments are:
888 #   transformed name of object being built, or empty string if no object
889 #   name of _LDADD/_LIBADD-type variable to examine
890 sub handle_lib_objects
891 {
892     local ($xname, $var) = @_;
893
894     die "programming error in handle_lib_objects"
895         if ! &variable_defined ($var);
896
897     # We recognize certain things that are commonly put in LIBADD or
898     # LDADD.
899     local ($lsearch);
900     local (@dep_list) = ();
901
902     foreach $lsearch (&variable_value_as_list ($var))
903     {
904         # Skip -lfoo and -Ldir.
905         next if $lsearch =~ /^-[lL]/;
906
907         # Assume we have a file of some sort, and push it onto the
908         # dependency list.  Autoconf substitutions are not pushed;
909         # rarely is a new dependency substituted into (eg) foo_LDADD
910         # -- but "bad things (eg -lX11) are routinely substituted.
911         push (@dep_list, $lsearch)
912             unless $lsearch =~ /^\@.*\@$/;
913
914         # Automatically handle @LIBOBJS@ and @ALLOCA@.  Basically this
915         # means adding entries to dep_files.
916         if ($lsearch eq '@LIBOBJS@')
917         {
918             if (! keys %libsources)
919             {
920                 &am_line_error ($var, "\@LIBOBJS\@ seen but never set in \`configure.in'");
921             }
922
923             local ($iter, $rewrite);
924             foreach $iter (keys %libsources)
925             {
926                 if ($iter =~ /\.h$/)
927                 {
928                     &require_file_with_line ($var, $FOREIGN, $iter);
929                 }
930                 elsif ($iter ne 'alloca.c')
931                 {
932                     ($rewrite = $iter) =~ s/\.c$/.P/;
933                     $dep_files{'.deps/' . $rewrite} = 1;
934                     &require_file_with_line ($var, $FOREIGN, $iter);
935                 }
936             }
937         }
938         elsif ($lsearch eq '@ALLOCA@')
939         {
940             &am_line_error ($var,
941                             "\@ALLOCA\@ seen but \`AC_FUNC_ALLOCA' not in \`configure.in'")
942                 if ! defined $libsources{'alloca.c'};
943             $dep_files{'.deps/alloca.P'} = 1;
944             &require_file_with_line ($var, $FOREIGN, 'alloca.c');
945         }
946     }
947
948     if ($xname ne '' && ! &variable_defined ($xname . '_DEPENDENCIES'))
949     {
950         &define_pretty_variable ($xname . '_DEPENDENCIES', @dep_list);
951     }
952 }
953
954 # Handle C programs.
955 sub handle_programs
956 {
957     local (@proglist) = &am_install_var ('-clean',
958                                          'programs', 'PROGRAMS',
959                                          'bin', 'sbin', 'libexec', 'pkglib',
960                                          'noinst', 'check');
961     return if ! @proglist;
962
963     # If a program is installed, this is required.  We only want this
964     # error to appear once.
965     &am_conf_error ("AC_ARG_PROGRAM must be used in configure.in")
966         unless $seen_arg_prog;
967     $seen_arg_prog = 1;
968
969     local ($obj) = &get_object_extension;
970     local ($one_file, $xname, $munge);
971
972     foreach $one_file (@proglist)
973     {
974         # Canonicalize names.
975         ($xname = $one_file) =~ tr/A-Za-z0-9_/_/c;
976         if ($xname ne $one_file)
977         {
978             local ($xt);
979             foreach $xt ('_LDADD', '_SOURCES', '_OBJECTS', '_DEPENDENCIES')
980             {
981                 &am_line_error ($one_file . $xt,
982                                 "invalid variable \`" . $one_file . $xt
983                                 . "'; should be \`" . $xname . $xt . "'")
984                     if &variable_defined ($one_file . $xt);
985             }
986         }
987
988         local ($linker) = &handle_source_transform ($xname, $one_file, $obj);
989
990         if (&variable_defined ($xname . "_LDADD"))
991         {
992             &handle_lib_objects ($xname, $xname . '_LDADD');
993         }
994         else
995         {
996             # User didn't define prog_LDADD override.  So do it.
997             &define_variable ($xname . '_LDADD', '$(LDADD)');
998
999             # This does a bit too much work.  But we need it to
1000             # generate _DEPENDENCIES when appropriate.
1001             &handle_lib_objects ($xname, 'LDADD')
1002                 if (&variable_defined ('LDADD'));
1003         }
1004
1005         # Determine program to use for link.
1006         local ($xlink);
1007         if (&variable_defined ($xname . '_LINK'))
1008         {
1009             $xlink = $xname . '_LINK';
1010         }
1011         else
1012         {
1013             $xlink = $linker ? $linker : 'LINK';
1014         }
1015
1016         $output_rules .=
1017             &file_contents_with_transform
1018                 ('s/\@PROGRAM\@/' . $one_file . '/go;'
1019                  . 's/\@XPROGRAM\@/' . $xname . '/go;'
1020                  . 's/\@XLINK\@/' . $xlink . '/go;',
1021                  'program');
1022     }
1023
1024     &handle_lib_objects ('', 'LDADD')
1025         if &variable_defined ('LDADD');
1026 }
1027
1028 # Handle libraries.
1029 sub handle_libraries
1030 {
1031     local (@liblist) = &am_install_var ('-no-all', '-clean',
1032                                         'libraries', 'LIBRARIES',
1033                                         'lib', 'pkglib', 'noinst', 'check');
1034     return if ! @liblist;
1035
1036     local (%valid) = &am_primary_prefixes ('LIBRARIES', 'lib', 'pkglib',
1037                                            'noinst', 'check');
1038     if (! $seen_ranlib)
1039     {
1040         local ($key);
1041         foreach $key (keys %valid)
1042         {
1043             if (&variable_defined ($key . '_LIBRARIES'))
1044             {
1045                 &am_line_error ($key . '_LIBRARIES', "library requires either \`AC_PROG_RANLIB' or `AM_PROG_LIBTOOL' in configure.in");
1046                 # Only get this error once.
1047                 $seen_ranlib = 1;
1048                 last;
1049             }
1050         }
1051     }
1052
1053     # Generate _LIBFILES variables.  Too bad we can't do this in
1054     # am_install_var.
1055     local ($onedir, $onelib);
1056     local (@outlist);
1057     local (@libfiles_list);
1058     foreach $onedir (keys %valid) 
1059     {
1060         if (&variable_defined ($onedir . '_LIBRARIES'))
1061         {
1062             @outlist = ();
1063             foreach $onelib (&variable_value_as_list ($onedir . '_LIBRARIES'))
1064             {
1065                 push (@outlist, 'lib' . $onelib . '.a');
1066             }
1067             &define_pretty_variable ($onedir . '_LIBFILES', @outlist);
1068         }
1069
1070         push (@libfiles_list, '$(' . $onedir . '_LIBFILES)')
1071             unless $onedir eq 'EXTRA';
1072     }
1073     push (@all, '$(LIBFILES)');
1074
1075     local ($obj) = &get_object_extension;
1076     local ($munge);
1077     local ($xlib);
1078     foreach $onelib (@liblist)
1079     {
1080         # Canonicalize names.
1081         ($xlib = $onelib) =~ tr/A-Za-z0-9_/_/c;
1082         if ($xlib ne $onelib)
1083         {
1084             local ($xt);
1085             foreach $xt ('_LIBADD', '_SOURCES', '_OBJECTS', '_DEPENDENCIES')
1086             {
1087                 &am_line_error ($one_file . $xt,
1088                                 "invalid variable \`" . $onelib . $xt
1089                                 . "'; should be \`" . $xlib . $xt . "'")
1090                     if &variable_defined ($onelib . $xt);
1091             }
1092         }
1093
1094         if (&variable_defined ($xlib . '_LIBADD'))
1095         {
1096             &handle_lib_objects ($xlib, $xlib . '_LIBADD');
1097         }
1098         else
1099         {
1100             # Generate support for conditional object inclusion in
1101             # libraries.
1102             &define_variable ($xlib . "_LIBADD", '');
1103         }
1104
1105         &handle_source_transform ($xlib, $onelib, $obj);
1106
1107         $output_rules .=
1108             &file_contents_with_transform ('s/\@LIBRARY\@/' . $onelib . '/go;'
1109                                            . 's/\@XLIBRARY\@/'
1110                                            . $xlib . '/go;',
1111                                            'library');
1112     }
1113
1114     # Turn "foo" into "libfoo.a" and include macro definition.
1115     grep (($_ = 'lib' . $_ . '.a') && 0, @liblist);
1116
1117     if (! &variable_defined ('LIBFILES'))
1118     {
1119         &define_pretty_variable ('LIBFILES ', @libfiles_list);
1120     }
1121
1122     if ($seen_libtool)
1123     {
1124         &define_variable ('AR', '$(LIBTOOL) archive');
1125         &define_variable ('RANLIB', '$(LIBTOOL) ranlib');
1126         &define_variable ('LCOMPILE', ('$(LIBTOOL) compile $(DEFS) $(INCLUDES)'
1127                                        . ' $(CPPFLAGS) $(CFLAGS)'));
1128     }
1129     else
1130     {
1131         &define_variable ('AR', 'ar');
1132         &define_variable ('RANLIB', '@RANLIB@');
1133     }
1134 }
1135
1136 # See if any _SOURCES variable were misspelled.
1137 sub check_typos
1138 {
1139     local ($varname, $primary);
1140     foreach $varname (keys %contents)
1141     {
1142         foreach $primary ('_SOURCES', '_LIBADD', '_LDADD', '_DEPENDENCIES')
1143         {
1144             if ($varname =~ /$primary$/ && ! $content_seen{$varname})
1145             {
1146                 &am_line_error ($varname,
1147                                 "invalid unused variable name: \`$varname'");
1148             }
1149         }
1150     }
1151 }
1152
1153 # Handle scripts.
1154 sub handle_scripts
1155 {
1156     # NOTE we no longer automatically clean SCRIPTS, because it is
1157     # useful to sometimes distribute scripts verbatim.  This happens
1158     # eg in Automake itself.
1159     &am_install_var ('scripts', 'SCRIPTS',
1160                      'bin', 'sbin', 'libexec', 'pkgdata',
1161                      'noinst', 'check');
1162
1163     # Set $scripts_installed if appropriate.  Make sure we only find
1164     # scripts which are actually installed -- this is why we can't
1165     # simply use the return value of am_install_var.
1166     local (%valid) = &am_primary_prefixes ('SCRIPTS', 'bin', 'sbin',
1167                                            'libexec', 'pkgdata',
1168                                            'noinst', 'check');
1169     local ($key);
1170     foreach $key (keys %valid)
1171     {
1172         if ($key ne 'noinst'
1173             && $key ne 'check'
1174             && &variable_defined ($key . '_SCRIPTS'))
1175         {
1176             $scripts_installed = 1;
1177             last;
1178         }
1179     }
1180
1181     if ($scripts_installed)
1182     {
1183         # If a program is installed, this is required.  We only want this
1184         # error to appear once.
1185         &am_conf_error ("AC_ARG_PROGRAM must be used in configure.in")
1186             unless $seen_arg_prog;
1187         $seen_arg_prog = 1;
1188     }
1189 }
1190
1191 # Search a file for a "version.texi" Texinfo include.  Return the name
1192 # of the include file if found, or the empty string if not.  A
1193 # "version.texi" file is actually any file whose name matches
1194 # "vers*.texi".
1195 sub grep_for_vers_texi
1196 {
1197     local ($filename) = @_;
1198
1199     if (! open (TEXI, $filename))
1200     {
1201         &am_error ("couldn't open \`$filename': $!");
1202         return '';
1203     }
1204     print "automake: reading $filename\n" if $verbose;
1205
1206     while (<TEXI>)
1207     {
1208         if (/^\@include\s+(vers[^.]*\.texi)\s*$/)
1209         {
1210             # Found it.
1211             close (TEXI);
1212             return $1;
1213         }
1214     }
1215
1216     close (TEXI);
1217     return '';
1218 }
1219
1220 # Handle all Texinfo source.
1221 sub handle_texinfo
1222 {
1223     &am_line_error ('TEXINFOS',
1224                     "\`TEXINFOS' is an anachronism; use \`info_TEXINFOS'")
1225         if &variable_defined ('TEXINFOS');
1226     return if (! &variable_defined ('info_TEXINFOS')
1227                && ! &variable_defined ('html_TEXINFOS'));
1228
1229     local (@texis) = &variable_value_as_list ('info_TEXINFOS');
1230
1231     local (@infos_list, @info_deps_list, @dvis_list, @texi_deps);
1232     local ($infobase, $info_cursor);
1233     local (%versions);
1234     local ($done) = 0;
1235     local ($vti);
1236     local ($tc_cursor, @texi_cleans);
1237     local ($canonical);
1238
1239     foreach $info_cursor (@texis)
1240     {
1241         ($infobase = $info_cursor) =~ s/\.texi(nfo)?$//;
1242
1243         # If 'version.texi' is referenced by input file, then include
1244         # automatic versioning capability.
1245         local ($vtexi)
1246             = &grep_for_vers_texi ($relative_dir . "/" . $info_cursor);
1247         if ($vtexi)
1248         {
1249             &am_error ("\`$vtexi', included in \`$info_cursor', also included in \`$versions{$vtexi}'")
1250                 if (defined $versions{$vtexi});
1251             $versions{$vtexi} = $info_cursor;
1252
1253             # We number the stamp-vti files.  This is doable since the
1254             # actual names don't matter much.  We only number starting
1255             # with the second one, so that the common case looks nice.
1256             $vti = 'vti' . ($done ? $done : '');
1257             &push_dist_common ($vtexi, 'stamp-' . $vti);
1258             push (@clean, $vti);
1259
1260             # Only require once.
1261             &require_conf_file_with_line ('info_TEXINFOS', $FOREIGN,
1262                                           'mdate-sh')
1263                 if ! $done;
1264             ++$done;
1265
1266             local ($conf_pat);
1267             ($conf_pat = $config_aux_dir) =~ s/(\W)/\\$1/g;
1268             $output_rules .=
1269                 &file_contents_with_transform
1270                     ('s/\@TEXI\@/' . $info_cursor . '/g; '
1271                      . 's/\@VTI\@/' . $vti . '/g; '
1272                      . 's/\@VTEXI\@/' . $vtexi . '/g;'
1273                      . 's,\@MDDIR\@,' . $conf_pat . ',g;',
1274                      'texi-version');
1275
1276             &push_phony_cleaners ($vti);
1277         }
1278
1279         # If user specified file_TEXINFOS, then use that as explicit
1280         # dependency list.
1281         @texi_deps = ();
1282         push (@texi_deps, $info_cursor);
1283         push (@texi_deps, $vtexi) if $vtexi;
1284
1285         # Canonicalize name first.
1286         ($canonical = $infobase) =~ tr/A-Za-z0-9_/_/c;
1287         if (&variable_defined ($canonical . "_TEXINFOS"))
1288         {
1289             push (@texi_deps, '$(' . $canonical . '_TEXINFOS)');
1290             &push_dist_common ('$(' . $canonical . '_TEXINFOS)');
1291         }
1292
1293         $output_rules .= ("\n" . $infobase . ".info: "
1294                           . join (' ', @texi_deps)
1295                           . "\n" . $infobase . ".dvi: "
1296                           . join (' ', @texi_deps)
1297                           . "\n\n");
1298
1299         # FIXME: this distributes too much.  How to find out precisely
1300         # which files will be generated by `makeinfo'?
1301         push (@infos_list, $infobase . '.info', $infobase . '.info[-0-9]*');
1302         push (@info_deps_list, $infobase . '.info');
1303         push (@dvis_list, $infobase . '.dvi');
1304
1305         # Generate list of things to clean for this target.  We do
1306         # this explicitly because otherwise too many things could be
1307         # removed.  In particular the ".log" extension might
1308         # reasonably be used in other contexts by the user.
1309         foreach $tc_cursor ('aux', 'cp', 'cps', 'dvi', 'fn', 'fns',
1310                             'ky', 'log', 'pg', 'toc', 'tp', 'vr', 'op')
1311         {
1312             push (@texi_cleans, $infobase . '.' . $tc_cursor);
1313         }
1314     }
1315
1316     # Some boilerplate.
1317     $output_vars .= &file_contents ('texinfos-vars');
1318     $output_rules .= &file_contents ('texinfos');
1319     push (@phony, 'install-info', 'uninstall-info');
1320
1321     # How to clean.
1322     $output_rules .= "\nmostlyclean-info:\n";
1323     &pretty_print_rule ("\trm -f", "\t  ", @texi_cleans);
1324     $output_rules .= ("\nclean-info:\n\ndistclean-info:\n\n"
1325                       . "maintainer-clean-info:\n\t"
1326                       . 'rm -f $(INFOS)' . "\n");
1327     &push_phony_cleaners ('info');
1328
1329     push (@suffixes, '.texi', '.texinfo', '.info', '.dvi');
1330
1331     if (! defined $options{'no-installinfo'})
1332     {
1333         push (@uninstall, 'uninstall-info');
1334         push (@installdirs, '$(infodir)');
1335         unshift (@install_data, 'install-info');
1336
1337         # Make sure documentation is made and installed first.  Use
1338         # $(INFO_DEPS), not 'info', because otherwise recursive makes
1339         # get run twice during "make all".
1340         unshift (@all, '$(INFO_DEPS)');
1341     }
1342     push (@clean, 'info');
1343     push (@info, '$(INFO_DEPS)');
1344     push (@dvi, '$(DVIS)');
1345
1346     &define_variable ("INFOS", join (' ', @infos_list));
1347     &define_variable ("INFO_DEPS", join (' ', @info_deps_list));
1348     &define_variable ("DVIS", join (' ', @dvis_list));
1349     # This next isn't strictly needed now -- the places that look here
1350     # could easily be changed to look in info_TEXINFOS.  But this is
1351     # probably better, in case noinst_TEXINFOS is ever supported.
1352     &define_variable ("TEXINFOS", $contents{'info_TEXINFOS'});
1353
1354     # Do some error checking.
1355     &require_file_with_line ('info_TEXINFOS', $FOREIGN, 'texinfo.tex');
1356 }
1357
1358 # Handle any man pages.
1359 sub handle_man_pages
1360 {
1361     &am_line_error ('MANS', "\`MANS' is an anachronism; use \`man_MANS'")
1362         if &variable_defined ('MANS');
1363     return if ! &variable_defined ('man_MANS');
1364
1365     # We generate the manpage install code by hand to avoid the use of
1366     # basename in the generated Makefile.
1367     local (@mans) = &variable_value_as_list ('man_MANS');
1368     local (%sections, %inames, %mbases, %secmap, %fullsecmap);
1369     local ($i) = 1;
1370     foreach (@mans)
1371     {
1372         # FIXME: statement without effect:
1373         /^(.*)\.([0-9])([a-z]*)$/;
1374         $sections{$2} = 1;
1375         $inames{$i} = $_;
1376         $mbases{$i} = $1;
1377         $secmap{$i} = $2;
1378         $fullsecmap{$i} = $2 . $3;
1379         $i++;
1380     }
1381
1382     # We don't really need this, but we use it in case we ever want to
1383     # support noinst_MANS.
1384     &define_variable ("MANS", $contents{'man_MANS'});
1385
1386     # Generate list of install dirs.
1387     $output_rules .= "install-man: \$(MANS)\n";
1388     $output_rules .= "\t\$(NORMAL_INSTALL)\n";
1389     foreach (keys %sections)
1390     {
1391         push (@installdirs, '$(mandir)/man' . $_)
1392             unless defined $options{'no-installman'};
1393         $output_rules .= ("\t" . '$(mkinstalldirs) $(mandir)/man'
1394                           . $_ . "\n");
1395     }
1396     push (@phony, 'install-man');
1397
1398     # Generate install target.
1399     local ($key);
1400     foreach $key (keys %inames)
1401     {
1402         $_ = $install_man_format;
1403         s/\@SECTION\@/$secmap{$key}/g;
1404         s/\@MAN\@/$inames{$key}/g;
1405         s/\@FULLSECT\@/$fullsecmap{$key}/g;
1406         s/\@MANBASE\@/$mbases{$key}/g;
1407         $output_rules .= $_;
1408     }
1409     $output_rules .= "\n";
1410
1411     $output_rules .= "uninstall-man:\n";
1412     foreach $key (keys %inames)
1413     {
1414         $_ = $uninstall_man_format;
1415         s/\@SECTION\@/$secmap{$key}/g;
1416         s/\@MAN\@/$inames{$key}/g;
1417         s/\@FULLSECT\@/$fullsecmap{$key}/g;
1418         s/\@MANBASE\@/$mbases{$key}/g;
1419         $output_rules .= $_;
1420     }
1421     $output_rules .= "\n";
1422     push (@phony, 'uninstall-man');
1423
1424     $output_vars .= &file_contents ('mans-vars');
1425
1426     if (! defined $options{'no-installman'})
1427     {
1428         push (@install_data, 'install-man');
1429         push (@uninstall, 'uninstall-man');
1430         push (@all, '$(MANS)');
1431     }
1432 }
1433
1434 # Handle DATA variables.
1435 sub handle_data
1436 {
1437     &am_install_var ('data', 'DATA', 'data', 'sysconf',
1438                      'sharedstate', 'localstate', 'pkgdata',
1439                      'noinst', 'check');
1440 }
1441
1442 # Handle TAGS.
1443 sub handle_tags
1444 {
1445     local ($tagging) = 0;
1446
1447     push (@phony, 'tags');
1448     if (&variable_defined ('SUBDIRS'))
1449     {
1450         $output_rules .= &file_contents ('tags');
1451         push (@phony, 'tags-recursive');
1452         $tagging = 1;
1453     }
1454     elsif ($dir_holds_sources
1455            || $dir_holds_headers
1456            || &variable_defined ('ETAGS_ARGS'))
1457     {
1458         $output_rules .= &file_contents ('tags-subd');
1459         $tagging = 1;
1460     }
1461
1462     if ($tagging)
1463     {
1464         $output_rules .= &file_contents ('tags-clean');
1465         push (@clean, 'tags');
1466         &push_phony_cleaners ('tags');
1467     }
1468     else
1469     {
1470         # Every Makefile must define some sort of TAGS rule.
1471         # Otherwise, it would be possible for a top-level "make TAGS"
1472         # to fail because some subdirectory failed.
1473         $output_rules .= "tags: TAGS\nTAGS:\n\n";
1474     }
1475 }
1476
1477 # Worker for handle_dist.
1478 sub handle_dist_worker
1479 {
1480     $output_rules .= 'distdir: $(DEP_DISTFILES)' . "\n";
1481
1482     # Initialization; only at top level.
1483     if ($relative_dir eq '.')
1484     {
1485         if ($strictness >= $GNITS)
1486         {
1487             # For Gnits users, this is pretty handy.  Look at 15 lines
1488             # in case some explanatory text is desirable.
1489             $output_rules .= '  @if sed 15q $(srcdir)/NEWS | fgrep -e "$(VERSION)" > /dev/null; then :; else \\
1490           echo "NEWS not updated; not releasing" 1>&2; \\
1491           exit 1; \\
1492         fi
1493 ';
1494         }
1495
1496
1497         $output_rules .=
1498             # Create dist directory.
1499             '   rm -rf $(distdir)
1500         mkdir $(distdir)
1501         -chmod 755 $(distdir)
1502 ';
1503
1504         # Only run automake in `dist' target if --include-deps not
1505         # specified.  That way the recipient of a distribution can run
1506         # "make dist" and not need Automake.
1507         if ($cmdline_use_dependencies)
1508         {
1509             $output_rules .=
1510                 (
1511                  # We need an absolute path for --output-dir.  Thus the
1512                  # weirdness.
1513                  '      here=`pwd`; distdir=`cd $(distdir) && pwd` \\
1514           && cd $(srcdir) \\
1515           && automake --include-deps --build-dir=$$here --srcdir-name=$(srcdir) --output-dir=$$distdir --strictness='
1516                  # Set strictness of output.
1517                  . $strictness_name . "\n"
1518                  );
1519         }
1520     }
1521
1522     # In loop, test for file existence because sometimes a file gets
1523     # included in DISTFILES twice.  For example this happens when a
1524     # single source file is used in building more than one program.
1525     # Also, there are situations in which "ln" can fail.  For instance
1526     # a file to distribute could actually be a cross-filesystem
1527     # symlink -- this can easily happen if "gettextize" was run on the
1528     # distribution.  Note that DISTFILES can contain a wildcard (for
1529     # info files, sigh), so we must use the echo trick.
1530     $output_rules .= '  @for file in `cd $(srcdir) && echo $(DISTFILES)`; do \\
1531           test -f $(distdir)/$$file \\
1532           || ln $(srcdir)/$$file $(distdir)/$$file 2> /dev/null \\
1533           || cp -p $(srcdir)/$$file $(distdir)/$$file; \\
1534         done
1535 ';
1536
1537     # If we have SUBDIRS, create all dist subdirectories and do
1538     # recursive build.
1539     if (&variable_defined ('SUBDIRS'))
1540     {
1541         # Test for directory existence here because previous automake
1542         # invocation might have created some directories.  Note that
1543         # we explicitly set distdir for the subdir make; that lets us
1544         # mix-n-match many automake-using packages into one large
1545         # package, and have "dist" at the top level do the right
1546         # thing.
1547         $output_rules .= '      for subdir in $(SUBDIRS); do            \\
1548           test -d $(distdir)/$$subdir           \\
1549           || mkdir $(distdir)/$$subdir          \\
1550           || exit 1;                            \\
1551           chmod 755 $(distdir)/$$subdir;        \\
1552           (cd $$subdir && $(MAKE) distdir=../$(distdir)/$$subdir distdir) \\
1553             || exit 1; \\
1554         done
1555 ';
1556     }
1557
1558     # If the target `dist-hook' exists, run it now.  This allows
1559     # users to do random weird things to the distribution before it is
1560     # packaged up.
1561     if (defined $contents{'dist-hook'})
1562     {
1563         $output_rules .= "\t\$(MAKE) dist-hook\n";
1564     }
1565
1566     push (@phony, 'distdir');
1567 }
1568
1569 # Handle 'dist' target.
1570 sub handle_dist
1571 {
1572     # Set up maint_charset.
1573     $local_maint_charset = $contents{'MAINT_CHARSET'}
1574         if &variable_defined ('MAINT_CHARSET');
1575     $maint_charset = $local_maint_charset
1576         if $relative_dir eq '.';
1577
1578     if (&variable_defined ('DIST_CHARSET'))
1579     {
1580         &am_line_error ('DIST_CHARSET',
1581                         "DIST_CHARSET defined but no MAINT_CHARSET defined")
1582             if ! $local_maint_charset;
1583         if ($relative_dir eq '.')
1584         {
1585             $dist_charset = $contents{'DIST_CHARSET'}
1586         }
1587         else
1588         {
1589             &am_line_error ('DIST_CHARSET',
1590                             "DIST_CHARSET can only be defined at top level");
1591         }
1592     }
1593
1594     # Look for common files that should be included in distribution.
1595     local ($cfile);
1596     foreach $cfile (@common_files)
1597     {
1598         if (-f ($relative_dir . "/" . $cfile))
1599         {
1600             &push_dist_common ($cfile);
1601         }
1602     }
1603
1604     # Keys of %dist_common are names of files to distributed.  We put
1605     # README first because it then becomes easier to make a
1606     # Usenet-compliant shar file (in these, README must be first).
1607     # FIXME: do more ordering of files here.
1608     local (@coms);
1609     if (defined $dist_common{'README'})
1610     {
1611         push (@coms, 'README');
1612         undef $dist_common{'README'};
1613     }
1614     push (@coms, sort keys %dist_common);
1615
1616     &define_pretty_variable ("DIST_COMMON", @coms);
1617     $output_vars .= "\n";
1618
1619     # Some boilerplate.
1620     $output_vars .= &file_contents ('dist-vars');
1621
1622     # Put these things in rules section so it is easier for whoever
1623     # reads Makefile.in.
1624     if (! &variable_defined ('distdir'))
1625     {
1626         if ($relative_dir eq '.')
1627         {
1628             $output_rules .= "\n" . 'distdir = $(PACKAGE)-$(VERSION)' . "\n";
1629         }
1630         else
1631         {
1632             $output_rules .= ("\n"
1633                               . 'distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)'
1634                               . "\n");
1635         }
1636     }
1637     if ($relative_dir ne '.')
1638     {
1639         $output_rules .= "\nsubdir = " . $relative_dir . "\n";
1640     }
1641
1642     # Generate 'dist' target, and maybe dist-shar / dist-zip / dist-tarZ.
1643     if ($relative_dir eq '.')
1644     {
1645         # Rule to check whether a distribution is viable.
1646         $output_rules .= ('# This target untars the dist file and tries a VPATH configuration.  Then
1647 # it guarantees that the distribution is self-contained by making another
1648 # tarfile.
1649 distcheck: dist
1650         rm -rf $(distdir)
1651         $(TAR) zxf $(distdir).tar.gz
1652         mkdir $(distdir)/=build
1653         mkdir $(distdir)/=inst
1654         dc_install_base=`cd $(distdir)/=inst && pwd`; \\'
1655                           . (defined $contents{'distcheck-hook'}
1656                              ? "\t\$(MAKE) distcheck-hook"
1657                              : '')
1658                           . '
1659         cd $(distdir)/=build \\
1660           && ../configure '
1661
1662                           . ($seen_gettext ? '--with-included-gettext ' : '')
1663                           . '--srcdir=.. --prefix=$$dc_install_base \\
1664           && $(MAKE) \\
1665           && $(MAKE) dvi \\
1666           && $(MAKE) check \\
1667           && $(MAKE) install \\
1668           && $(MAKE) installcheck \\
1669           && $(MAKE) dist
1670         rm -rf $(distdir)
1671         @echo "========================"; \\
1672         echo "$(distdir).tar.gz is ready for distribution"; \\
1673         echo "========================"
1674 ');
1675
1676         $output_rules .= 'dist: distdir' . "\n\t";
1677         # Ignore return result from chmod, because it might give an
1678         # error if we chmod a symlink.
1679         $output_rules .= '-chmod -R a+r $(distdir)' . "\n\t";
1680         $output_rules .= '$(TAR) chozf $(distdir).tar.gz $(distdir)';
1681         $output_rules .= "\n\t" . 'rm -rf $(distdir)' . "\n";
1682
1683         if (defined $options{'dist-shar'})
1684         {
1685             $output_rules .= 'dist-shar: distdir' . "\n\t";
1686             # Ignore return result from chmod, because it might give
1687             # an error if we chmod a symlink.
1688             $output_rules .= '-chmod -R a+r $(distdir)' . "\n\t";
1689             $output_rules .= 'shar $(distdir) | gzip > $(distdir).shar.gz';
1690             $output_rules .= "\n\t" . 'rm -rf $(distdir)' . "\n";
1691         }
1692
1693         if (defined $options{'dist-zip'})
1694         {
1695             $output_rules .= 'dist-zip: distdir' . "\n\t";
1696             $output_rules .= '-chmod -R a+r $(distdir)' . "\n\t";
1697             $output_rules .= 'zip -rq $(distdir).zip $(distdir)';
1698             $output_rules .= "\n\t" . 'rm -rf $(distdir)' . "\n";
1699         }
1700
1701         if (defined $options{'dist-tarZ'})
1702         {
1703             $output_rules .= 'dist-tarZ: distdir' . "\n\t";
1704             $output_rules .= '-chmod -R a+r $(distdir)' . "\n\t";
1705             $output_rules .= '$(TAR) chf - $(distdir) | compress -c > $(distdir).tar.Z';
1706             $output_rules .= "\n\t" . 'rm -rf $(distdir)' . "\n";
1707         }
1708     }
1709
1710     # Generate distdir target.
1711     &handle_dist_worker;
1712 }
1713
1714 # Scan a single dependency file and rewrite the dependencies as
1715 # appropriate.  Essentially this means:
1716 # * Clean out absolute dependencies which are not desirable.
1717 # * Rewrite other dependencies to be relative to $(top_srcdir).
1718 sub scan_dependency_file
1719 {
1720     local ($depfile) = @_;
1721
1722     if (! open (DEP_FILE, $depfile))
1723     {
1724         &am_error ("couldn't open \`$depfile': $!");
1725         return;
1726     }
1727     print "automake: reading $depfile\n" if $verbose;
1728
1729     local ($first_line) = 1;
1730     local ($last_line) = 0;
1731     local ($target, @dependencies);
1732     local ($one_dep, $xform);
1733
1734     local ($srcdir_rx, $fixup_rx, $fixup2_rx);
1735     ($srcdir_rx = $srcdir_name) =~ s/(\W)/\\$1/g;
1736
1737     # If the top srcdir is absolute, then the current directory is
1738     # just relative_dir.
1739     ($fixup_rx = $srcdir_name . '/' . $relative_dir . '/') =~ s/(\W)/\\$1/g;
1740
1741     # But if the top srcdir is relative, then we need some dots first.
1742     ($fixup2_rx = ($srcdir_name . '/' . $top_builddir . '/'
1743                    . $relative_dir . '/')) =~ s/(\W)/\\$1/g;
1744
1745     while (<DEP_FILE>)
1746     {
1747         if ($last_line)
1748         {
1749             # If LAST_LINE set then we've already seen what we thought
1750             # was the last line.
1751             goto bad_format;
1752         }
1753         next if (/$WHITE_PATTERN/o);
1754         chop;
1755         if (! s/\\$//)
1756         {
1757             # No trailing "\" means this should be the last line.
1758             $last_line = 1;
1759         }
1760
1761         if ($first_line)
1762         {
1763             if (! /^([^:]+:)(.+)$/)
1764             {
1765               bad_format:
1766                 &am_error ("\`$depfile' has incorrect format");
1767                 close (DEP_FILE);
1768                 return;
1769             }
1770
1771             $target = $1;
1772             $_ = $2;
1773
1774             $first_line = 0;
1775         }
1776
1777         foreach $one_dep (split (' ', $_))
1778         {
1779             if ($one_dep =~ /^$fixup_rx/)
1780             {
1781                 # The dependency points to the current directory in
1782                 # some way.
1783                 ($xform = $one_dep) =~ s/^$fixup_rx//;
1784                 push (@dependencies, $xform);
1785             }
1786             elsif ($one_dep =~ /^$fixup2_rx/)
1787             {
1788                 # The dependency points to the current directory in
1789                 # some way.
1790                 ($xform = $one_dep) =~ s/^$fixup2_rx//;
1791                 push (@dependencies, $xform);
1792             }
1793             elsif ($one_dep =~ /^$srcdir_rx\//)
1794             {
1795                 # The dependency is in some other directory in the package.
1796                 ($xform = $one_dep) =~ s/^$srcdir_rx/$top_builddir/;
1797                 push (@dependencies, $xform);
1798             }
1799             elsif ($one_dep =~ /^\//)
1800             {
1801                 # Absolute path; ignore.
1802             }
1803             else
1804             {
1805                 # Anything else is assumed to be correct.
1806                 push (@dependencies, $one_dep);
1807             }
1808         }
1809     }
1810
1811     &pretty_print_rule ($target, "\t", @dependencies);
1812
1813     close (DEP_FILE);
1814 }
1815
1816 # Handle auto-dependency code.
1817 sub handle_dependencies
1818 {
1819     if ($use_dependencies)
1820     {
1821         # Include GNU-make-specific auto-dep code.
1822         if ($dir_holds_sources)
1823         {
1824             &define_pretty_variable ('DEP_FILES', sort keys %dep_files);
1825             $output_rules .= &file_contents ('depend');
1826             push (@clean, 'depend');
1827             &push_phony_cleaners ('depend');
1828             $output_rules .=
1829                 &file_contents_with_transform ('s/\@EXT\@/.c/g;'
1830                                                . 's/\@MKDEP\@/MKDEP/g',
1831                                                'depend2');
1832             local ($ext);
1833             local ($need_cxx) = 0;
1834             foreach $ext (keys %cxx_extensions)
1835             {
1836                 $output_rules .=
1837                     &file_contents_with_transform ('s/\@EXT\@/' . $ext .'/g;'
1838                                                    . 's/\@MKDEP\@/CXXMKDEP/g',
1839                                                    'depend2');
1840                 $need_cxx = 1;
1841             }
1842             if ($need_cxx)
1843             {
1844                 &define_variable ('CXXMKDEP', '$(CXX) -MM $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS)');
1845             }
1846         }
1847     }
1848     else
1849     {
1850         # FIXME: consider requiring --build-dir here.  What about case
1851         # where this is done via an option?
1852
1853         # Include any auto-generated deps that are present.  Note that
1854         # $build_directory ends in a "/".
1855         if (-d ($build_directory . $relative_dir . "/.deps")
1856             && -f ($build_directory . $relative_dir . "/.deps/.P"))
1857         {
1858             local ($depfile);
1859             local ($gpat) = $build_directory . $relative_dir . "/.deps/*.P";
1860
1861             foreach $depfile (<${gpat}>)
1862             {
1863                 &scan_dependency_file ($depfile);
1864             }
1865
1866             $output_rules .= "\n";
1867         }
1868     }
1869 }
1870
1871 # Handle subdirectories.
1872 sub handle_subdirs
1873 {
1874     if (! &variable_defined ('SUBDIRS'))
1875     {
1876         &am_conf_error
1877             ("ud_GNU_GETTEXT in configure.in but SUBDIRS not defined")
1878                 if $seen_gettext && $relative_dir eq '.';
1879         return;
1880     }
1881
1882     &require_file_with_conf_line ($ac_gettext_line, $FOREIGN, 'ABOUT-NLS')
1883         if $seen_gettext;
1884
1885     return if ! &variable_defined ('SUBDIRS');
1886
1887     # Make sure each directory mentioned in SUBDIRS actually exists.
1888     local ($dir);
1889     foreach $dir (&variable_value_as_list ('SUBDIRS'))
1890     {
1891         # Skip directories substituted by configure.
1892         next if $dir =~ /^\@.*\@$/;
1893         &am_line_error ('SUBDIRS',
1894                         "required directory $relative_dir/$dir does not exist")
1895             if ! -d $relative_dir . '/' . $dir;
1896     }
1897
1898     $output_rules .= &file_contents ('subdirs');
1899
1900     # Push a bunch of phony targets.
1901     local ($phonies);
1902     foreach $phonies ('-data', '-exec', 'dirs')
1903     {
1904         push (@phony, 'install' . $phonies . '-recursive');
1905         push (@phony, 'uninstall' . $phonies . '-recursive');
1906     }
1907     foreach $phonies ('all', 'check', 'installcheck', 'info', 'dvi')
1908     {
1909         push (@phony, $phonies . '-recursive');
1910     }
1911     &push_phony_cleaners ('recursive');
1912
1913     push (@check, "check-recursive");
1914     push (@installcheck, "installcheck-recursive");
1915     push (@info, "info-recursive");
1916     push (@dvi, "dvi-recursive");
1917
1918     $recursive_install = 1;
1919 }
1920
1921 # Handle aclocal.m4.
1922 sub handle_aclocal_m4
1923 {
1924     local ($regen_aclocal) = 0;
1925     if (-f 'aclocal.m4')
1926     {
1927         &define_variable ("ACLOCAL", "aclocal.m4");
1928         &push_dist_common ('aclocal.m4');
1929
1930         if (open (ACLOCAL, '< aclocal.m4'))
1931         {
1932             local ($line);
1933             $line = <ACLOCAL>;
1934             close (ACLOCAL);
1935
1936             if ($line =~ 'generated automatically by aclocal')
1937             {
1938                 $regen_aclocal = 1;
1939             }
1940         }
1941     }
1942
1943     local ($acinclude) = 0;
1944     if (-f 'acinclude.m4')
1945     {
1946         $regen_aclocal = 1;
1947         $acinclude = 1;
1948     }
1949
1950     # Note that it might be possible that aclocal.m4 doesn't exist but
1951     # should be auto-generated.  This case probably isn't very
1952     # important.
1953     if ($regen_aclocal)
1954     {
1955         $output_rules .= ("aclocal.m4: "
1956                           . ($seen_maint_mode ? "\@MAINT\@" : "")
1957                           . "configure.in"
1958                           . ($acinclude ? ' acinclude.m4' : '')
1959                           . "\n\t"
1960                           . 'cd $(srcdir) && aclocal'
1961                           . "\n");
1962     }
1963 }
1964
1965 # Handle remaking and configure stuff.
1966 sub handle_configure
1967 {
1968     # If SUBDIRS defined, require AC_PROG_MAKE_SET.
1969     &am_line_error ('SUBDIRS', "AC_PROG_MAKE_SET must be used in configure.in")
1970         if &variable_defined ('SUBDIRS') && ! $seen_make_set;
1971
1972     local ($top_reldir);
1973     if ($relative_dir ne '.')
1974     {
1975         # In subdirectory.
1976         $output_rules .= &file_contents ('remake-subd');
1977         $top_reldir = '../';
1978     }
1979     else
1980     {
1981         &handle_aclocal_m4;
1982         $output_rules .= &file_contents_with_transform ('s/\@STRICTNESS\@/'
1983                                                         . $strictness_name
1984                                                         . '/g',
1985                                                         'remake');
1986
1987         &am_error
1988             ("\`install.sh' is an anachronism; use \`install-sh' instead")
1989                 if -f $relative_dir . '/install.sh';
1990
1991         # If we have a configure header, require it.
1992         if ($config_header)
1993         {
1994             # FIXME: this restriction should be lifted.
1995             # FIXME: first see if it is even needed as-is.
1996             &am_conf_line_error ($config_header_line,
1997                                  "argument to AC_CONFIG_HEADER contains \`/'\n")
1998                 if ($config_header =~ /\//);
1999
2000             &require_file_with_conf_line ($config_header_line,
2001                                           $FOREIGN, $config_header);
2002
2003             # Header defined and in this directory.
2004             if (-f 'acconfig.h')
2005             {
2006                 &define_variable ("ACCONFIG", "acconfig.h");
2007                 &push_dist_common ('acconfig.h');
2008             }
2009             if (-f $config_name . '.top')
2010             {
2011                 &define_variable ("CONFIG_TOP", "${config_name}.top");
2012                 &push_dist_common ($config_name . '.top');
2013             }
2014             if (-f $config_name . '.bot')
2015             {
2016                 &define_variable ("CONFIG_BOT", "${config_name}.bot");
2017                 &push_dist_common ($config_name . '.bot');
2018             }
2019
2020             &require_file_with_conf_line ($config_header_line, $FOREIGN,
2021                                           'stamp-h.in');
2022
2023             $output_rules .= &file_contents ('remake-hdr');
2024             &define_variable ("CONFIG_HEADER_IN", "${config_header}");
2025         }
2026
2027         $top_reldir = '';
2028     }
2029
2030     # Set location of mkinstalldirs.
2031     if ($config_aux_dir ne '.' && $config_aux_dir ne '')
2032     {
2033         &define_variable ('mkinstalldirs', $config_aux_dir . '/mkinstalldirs');
2034     }
2035     else
2036     {
2037         &define_variable ('mkinstalldirs', '$(top_srcdir)/mkinstalldirs');
2038     }
2039
2040     &am_line_error ('CONFIG_HEADER',
2041                     "\`CONFIG_HEADER' is an anachronism; now determined from \`configure.in'")
2042         if &variable_defined ('CONFIG_HEADER');
2043
2044     # Generate CONFIG_HEADER define.
2045     &define_variable ("CONFIG_HEADER", "${top_builddir}/${config_name}")
2046         if $config_name;
2047
2048     # Now look for other files in this directory which must be remade
2049     # by config.status, and generate rules for them.
2050     local (@actual_other_files) = ();
2051     local ($file, $local, $input);
2052     foreach $file (@other_input_files)
2053     {
2054         # Skip files not in this directory, any Makefile, and the
2055         # config header.  These last two must be handled specially.
2056         next unless &dirname ($file) eq $relative_dir;
2057         next if $file eq $top_builddir . '/' . $config_name;
2058         ($local = $file) =~ s/^.*\///;
2059         next if $local eq 'Makefile';
2060
2061         if ($local =~ /^(.*):(.*)$/)
2062         {
2063             # This is the ":" syntax of AC_OUTPUT.
2064             $input = $2;
2065             $local = $1;
2066         }
2067         else
2068         {
2069             # Normal usage.
2070             $input = $local . '.in';
2071         }
2072         # FIXME: when using autoconf ":" syntax, should we set CONFIG_FILES
2073         # to $local:$input?
2074         $output_rules .= ($local . ': '
2075                           . '$(top_builddir)/config.status ' . $input . "\n"
2076                           . "\t"
2077                           . 'cd $(top_builddir) && CONFIG_FILES='
2078                           . ($relative_dir eq '.' ? '' : '$(subdir)/')
2079                           . '$@ CONFIG_HEADERS= ./config.status'
2080                           . "\n");
2081         push (@actual_other_files, $local);
2082
2083         &require_file_with_conf_line ($ac_output_line, $FOREIGN,
2084                                       $input);
2085     }
2086
2087     # These files get removed by "make clean".
2088     &define_pretty_variable ('CONFIG_CLEAN_FILES', @actual_other_files);
2089 }
2090
2091 # Handle C headers.
2092 sub handle_headers
2093 {
2094     $dir_holds_headers = &am_install_var ('header', 'HEADERS', 'include',
2095                                           'oldinclude', 'pkginclude',
2096                                           'noinst', 'check');
2097 }
2098
2099 sub handle_gettext
2100 {
2101     return if ! $seen_gettext || $relative_dir ne '.';
2102
2103     if (&variable_defined ('SUBDIRS'))
2104     {
2105         &am_line_error
2106             ('SUBDIRS',
2107              "ud_GNU_GETTEXT in configure.in but \`po' not in SUBDIRS")
2108                 if $contents{'SUBDIRS'} !~ /\bpo\b/;
2109         &am_line_error
2110             ('SUBDIRS',
2111              "ud_GNU_GETTEXT in configure.in but \`intl' not in SUBDIRS")
2112                 if $contents{'SUBDIRS'} !~ /\bintl\b/;
2113     }
2114
2115     # Ensure that each language in ALL_LINGUAS has a .po file, and
2116     # each po file is mentioned in ALL_LINGUAS.
2117     if ($seen_linguas)
2118     {
2119         local (%linguas) = ();
2120         grep ($linguas{$_} = 1, split (' ', $all_linguas));
2121
2122         foreach (<po/*.po>)
2123         {
2124             s/^po\///;
2125             s/\.po$//;
2126
2127             &am_line_error ($all_linguas_line,
2128                             ("po/$_.po exists but \`$_' not in \`ALL_LINGUAS'"))
2129                 if ! $linguas{$_};
2130         }
2131
2132         foreach (keys %linguas)
2133         {
2134             &am_line_error ($all_linguas_line,
2135                             "$_ in \`ALL_LINGUAS' but po/$_.po does not exist")
2136                 if ! -f "po/$_.po";
2137         }
2138     }
2139     else
2140     {
2141         &am_error ("ud_GNU_GETTEXT in configure.in but \`ALL_LINGUAS' not defined");
2142     }
2143 }
2144
2145 # Handle footer elements.
2146 sub handle_footer
2147 {
2148     if ($contents{'SOURCES'})
2149     {
2150         # NOTE don't use define_pretty_variable here, because
2151         # $contents{...} is already defined.
2152         $output_vars .= 'SOURCES = ' . $contents{'SOURCES'} . "\n";
2153     }
2154     if ($contents{'OBJECTS'})
2155     {
2156         # NOTE don't use define_pretty_variable here, because
2157         # $contents{...} is already defined.
2158         $output_vars .= 'OBJECTS = ' . $contents{'OBJECTS'} . "\n";
2159     }
2160     if ($contents{'SOURCES'} || $contents{'OBJECTS'})
2161     {
2162         $output_vars .= "\n";
2163     }
2164
2165     if (defined $contents{'SUFFIXES'})
2166     {
2167         # Push actual suffixes, and not $(SUFFIXES).  Some versions of
2168         # make do not like variable substitutions on the .SUFFIXES
2169         # line.
2170         push (@suffixes, split (' ', $contents{'SUFFIXES'}));
2171     }
2172
2173     $output_trailer .= ".SUFFIXES:\n";
2174     if (@suffixes)
2175     {
2176         $output_trailer .= ".SUFFIXES: " . join (' ', @suffixes) . "\n";
2177     }
2178     $output_trailer .= &file_contents ('footer');
2179 }
2180
2181 # Deal with installdirs target.
2182 sub handle_installdirs
2183 {
2184     # GNU Makefile standards recommend this.
2185     $output_rules .= ("installdirs:"
2186                       . ($recursive_install
2187                          ? " installdirs-recursive\n"
2188                          : "\n"));
2189     push (@phony, 'installdirs');
2190     if (@installdirs)
2191     {
2192         &pretty_print_rule ("\t" . '$(mkinstalldirs) ', "\t\t",
2193                             @installdirs);
2194     }
2195     $output_rules .= "\n";
2196 }
2197
2198 # There are several targets which need to be merged.  This is because
2199 # their complete definition is compiled from many parts.  Note that we
2200 # avoid double colon rules, otherwise we'd use them instead.
2201 sub handle_merge_targets
2202 {
2203     # There are a few install-related variables that you should not define.
2204     local ($var);
2205     foreach $var ('PRE_INSTALL', 'POST_INSTALL', 'NORMAL_INSTALL')
2206     {
2207         if (&variable_defined ($var))
2208         {
2209             &am_line_error ($var, "\`$var' should not be defined");
2210         }
2211     }
2212
2213     push (@all, 'Makefile');
2214     push (@all, $config_name)
2215         if $config_name && &dirname ($config_name) eq $relative_dir;
2216
2217     &do_one_merge_target ('info', @info);
2218     &do_one_merge_target ('dvi', @dvi);
2219
2220     if (! &variable_defined ('SUBDIRS') || $relative_dir ne '.')
2221     {
2222         # 'check' must depend on 'all', but not at top level.
2223         # Ditto install.
2224         unshift (@check, 'all');
2225         unshift (@install, 'all');
2226     }
2227     &do_one_merge_target ('check', @check);
2228     &do_one_merge_target ('installcheck', @installcheck);
2229
2230     # Handle the various install targets specially.  We do this so
2231     # that (eg) "make install-exec" will run "install-exec-recursive"
2232     # if required, but "make install" won't run it twice.  Step one is
2233     # to see if the user specified local versions of any of the
2234     # targets we handle.  "all" is treated as one of these since
2235     # "install" can run it.
2236     push (@install_exec, 'install-exec-local')
2237         if defined $contents{'install-exec-local'};
2238     push (@install_data, 'install-data-local')
2239         if defined $contents{'install-data-local'};
2240     push (@uninstall, 'uninstall-local')
2241         if defined $contents{'uninstall-local'};
2242     local ($utarg);
2243     foreach $utarg ('uninstall-data-local', 'uninstall-data-hook',
2244                     'uninstall-exec-local', 'uninstall-exec-hook')
2245     {
2246         if (defined $contents{$utarg})
2247         {
2248             local ($x);
2249             ($x = $utarg) =~ s/(data|exec)-//;
2250             &am_line_error ($utarg, "use \`$x', not \`$utarg'");
2251         }
2252     }
2253     push (@all, 'all-local')
2254         if defined $contents{'all-local'};
2255
2256     if (defined $contents{'install-local'})
2257     {
2258         &am_line_error ('install-local',
2259                         "use \`install-data' or \`install-exec', not \`install'");
2260     }
2261
2262     # Step two: if we are doing recursive makes, write out the
2263     # appropriate rules.
2264     local (@install);
2265     if ($recursive_install)
2266     {
2267         push (@install, 'install-recursive');
2268
2269         if (@all)
2270         {
2271             local (@hackall) = ();
2272             if ($config_name && &dirname ($config_name) eq $relative_dir)
2273             {
2274
2275                 # This is kind of a hack, but I couldn't see a better
2276                 # way to handle it.  In this particular case, we need
2277                 # to make sure config.h is built before we recurse.
2278                 # We can't do this by changing the order of
2279                 # dependencies to the "all" because that breaks when
2280                 # using parallel makes.  Instead we handle things
2281                 # explicitly.
2282                 $output_rules .= ('all-recursive-hack: $(CONFIG_HEADER)'
2283                                   . "\n\t" . '$(MAKE) all-recursive'
2284                                   . "\n\n");
2285                 push (@hackall, 'all-recursive-hack');
2286                 push (@phony, 'all-recursive-hack');
2287             }
2288             else
2289             {
2290                 push (@hackall, 'all-recursive');
2291             }
2292
2293             $output_rules .= ('all-am: '
2294                               . join (' ', @all)
2295                               . "\n\n");
2296             @all = @hackall;
2297             push (@all, 'all-am');
2298             push (@phony, 'all-am');
2299         }
2300         else
2301         {
2302             @all = ('all-recursive');
2303         }
2304         if (@install_exec)
2305         {
2306             $output_rules .= ('install-exec-am: '
2307                               . join (' ', @install_exec)
2308                               . "\n\n");
2309             @install_exec = ('install-exec-recursive', 'install-exec-am');
2310             push (@install, 'install-exec-am');
2311             push (@phony, 'install-exec-am');
2312         }
2313         else
2314         {
2315             @install_exec = ('install-exec-recursive');
2316         }
2317         if (@install_data)
2318         {
2319             $output_rules .= ('install-data-am: '
2320                               . join (' ', @install_data)
2321                               . "\n\n");
2322             @install_data = ('install-data-recursive', 'install-data-am');
2323             push (@install, 'install-data-am');
2324             push (@phony, 'install-data-am');
2325         }
2326         else
2327         {
2328             @install_data = ('install-data-recursive');
2329         }
2330         if (@uninstall)
2331         {
2332             $output_rules .= ('uninstall-am: '
2333                               . join (' ', @uninstall)
2334                               . "\n\n");
2335             @uninstall = ('uninstall-recursive', 'uninstall-am');
2336             push (@phony, 'uninstall-am');
2337         }
2338         else
2339         {
2340             @uninstall = ('uninstall-recursive');
2341         }
2342     }
2343
2344     # Step three: print definitions users can use.  Code below knows
2345     # that install-exec is done before install-data, beware.
2346     $output_rules .= ("install-exec: "
2347                       . join (' ', @install_exec)
2348                       . "\n");
2349     $output_rules .= "\t\$(NORMAL_INSTALL)\n";
2350     if (defined $contents{'install-exec-hook'})
2351     {
2352         $output_rules .= "\t" . '$(MAKE) install-exec-hook' . "\n";
2353     }
2354     $output_rules .= "\n";
2355     push (@install, 'install-exec') if !$recursive_install;
2356     push (@phony, 'install-exec');
2357
2358     $output_rules .= ("install-data: "
2359                       . join (' ', @install_data)
2360                       . "\n");
2361     $output_rules .= "\t\$(NORMAL_INSTALL)\n";
2362     if (defined $contents{'install-data-hook'})
2363     {
2364         $output_rules .= "\t" . '$(MAKE) install-data-hook' . "\n";
2365     }
2366     $output_rules .= "\n";
2367     push (@install, 'install-data') if !$recursive_install;
2368     push (@phony, 'install-data');
2369
2370     # If no dependencies for 'install', add 'all'.  Why?  That way
2371     # "make install" at top level of distclean'd distribution won't
2372     # fail because stuff in 'lib' fails to build.
2373     if (! @install || (scalar (@install) == 2
2374                        && $install[0] eq 'install-exec'
2375                        && $install[1] eq 'install-data'))
2376     {
2377         push (@install, 'all');
2378     }
2379     $output_rules .= ('install: '
2380                       . join (' ', @install)
2381                       # Use "@:" as empty command so nothing prints.
2382                       . "\n\t\@:"
2383                       . "\n\n"
2384                       . 'uninstall: '
2385                       . join (' ', @uninstall)
2386                       . "\n\n");
2387     push (@phony, 'install', 'uninstall');
2388
2389     $output_rules .= ('all: '
2390                       . join (' ', @all)
2391                       . "\n\n");
2392     push (@phony, 'all');
2393
2394     # Generate the new 'install-strip' target.
2395     $output_rules .= ("install-strip:\n\t"
2396                       . '$(MAKE) INSTALL_PROGRAM=\'$(INSTALL_PROGRAM) -s\' install'
2397                       . "\n");
2398 }
2399
2400 # Helper for handle_merge_targets.
2401 sub do_one_merge_target
2402 {
2403     local ($name, @values) = @_;
2404
2405     if (defined $contents{$name . '-local'})
2406     {
2407         # User defined local form of target.  So include it.
2408         push (@values, $name . '-local');
2409         push (@phony, $name . '-local');
2410     }
2411
2412     $output_rules .= $name . ":";
2413     if (@values)
2414     {
2415         $output_rules .= ' ' . join (' ', @values);
2416     }
2417     $output_rules .= "\n\n";
2418     push (@phony, $name);
2419 }
2420
2421 # Handle all 'clean' targets.
2422 sub handle_clean
2423 {
2424     push (@clean, 'generic');
2425     $output_rules .= &file_contents ('clean');
2426     &push_phony_cleaners ('generic');
2427
2428     local ($target) = $recursive_install ? 'clean-am' : 'clean';
2429     &do_one_clean_target ($target, 'mostly', '', @clean);
2430     &do_one_clean_target ($target, '', 'mostly', @clean);
2431     &do_one_clean_target ($target, 'dist', '', @clean);
2432     &do_one_clean_target ($target, 'maintainer-', 'dist', @clean);
2433
2434     push (@phony, 'clean', 'mostlyclean', 'distclean', 'maintainer-clean');
2435
2436     local (@deps);
2437     if ($recursive_install)
2438     {
2439         @deps = ('am', 'recursive');
2440         &do_one_clean_target ('', 'mostly', '', @deps);
2441         &do_one_clean_target ('', '', '', @deps);
2442         &do_one_clean_target ('', 'dist', '', @deps);
2443         &do_one_clean_target ('', 'maintainer-', '', @deps);
2444     }
2445 }
2446
2447 # Helper for handle_clean.
2448 sub do_one_clean_target
2449 {
2450     local ($target, $name, $last_name, @deps) = @_;
2451
2452     # Special case: if target not passed, then don't generate
2453     # dependency on next "lower" clean target (eg no
2454     # clean<-mostlyclean derivation).  In this case the target is
2455     # implicitly known to be 'clean'.
2456     local ($flag) = $target;
2457     $target = 'clean' if ! $flag;
2458
2459     grep (($_ = $name . 'clean-' . $_) && 0, @deps);
2460     if ($flag)
2461     {
2462         if ($last_name || $name ne 'mostly')
2463         {
2464             push (@deps, $last_name . $target . " ");
2465         }
2466     }
2467     # FIXME: not sure if I like the tabs here.
2468     &pretty_print_rule ($name . $target . ": ", "\t\t", @deps);
2469
2470     # FIXME: shouldn't we really print these messages before running
2471     # the dependencies?
2472     if ($name . $target eq 'maintainer-clean')
2473     {
2474         # Print a special warning.
2475         $output_rules .=
2476             ("\t\@echo \"This command is intended for maintainers to use;\"\n"
2477              . "\t\@echo \"it deletes files that may require special "
2478              . "tools to rebuild.\"\n");
2479
2480         $output_rules .= "\trm -f config.status\n"
2481             if $relative_dir eq '.';
2482     }
2483     elsif ($name . $target eq 'distclean')
2484     {
2485         $output_rules .= "\trm -f config.status\n";
2486     }
2487     $output_rules .= "\n";
2488 }
2489
2490 # Handle .PHONY target.
2491 sub handle_phony
2492 {
2493     &pretty_print_rule ('.PHONY:', "", @phony);
2494     $output_rules .= "\n";
2495 }
2496
2497 # Handle TESTS variable and other checks.
2498 sub handle_tests
2499 {
2500     if (defined $options{'dejagnu'})
2501     {
2502         push (@check, 'check-DEJAGNU');
2503         push (@phony, 'check-DEJAGNU');
2504         $output_rules .= &file_contents ('dejagnu') . "\n";
2505         # Note that in the rule we don't directly generate site.exp to
2506         # avoid the possibility of a corrupted site.exp if make is
2507         # interrupted.  Jim Meyering has some useful text on this
2508         # topic.
2509         $output_rules .= ("site.exp: Makefile\n"
2510                           . "\t\@echo 'Making a new site.exp file...'\n"
2511                           . "\t-\@rm -f site.bak\n"
2512                           . "\t\@echo '## these variables are automatically generated by make ##' > \$\@-t\n"
2513                           . "\t\@echo '# Do not edit here.  If you wish to override these values' >> \$\@-t\n"
2514                           . "\t\@echo '# edit the last section' >> \$\@-t\n"
2515                           . "\t\@echo 'set tool \$(DEJATOOL)' >> \$\@-t\n"
2516                           . "\t\@echo 'set srcdir \$(srcdir)' >> \$\@-t\n"
2517                           . "\t\@echo 'set objdir' \`pwd\` >> \$\@-t\n");
2518
2519         # Extra stuff for AC_CANONICAL_*
2520         local (@whatlist) = ();
2521         if ($seen_canonical)
2522         {
2523             push (@whatlist, 'host')
2524         }
2525
2526         # Extra stuff only for AC_CANONICAL_SYSTEM.
2527         if ($seen_canonical == $AC_CANONICAL_SYSTEM)
2528         {
2529             push (@whatlist, 'target', 'build');
2530         }
2531
2532         local ($c1, $c2);
2533         foreach $c1 (@whatlist)
2534         {
2535             foreach $c2 ('alias', 'triplet')
2536             {
2537                 $output_rules .= "\t\@echo 'set ${c1}_${c2} \$(${c1}_${c2})' >> \$\@-t\n";
2538             }
2539         }
2540
2541         $output_rules .= ("\t\@echo '## All variables above are generated by configure. Do Not Edit ##' >> \$\@-t\n"
2542                           . "\t-\@sed '1,/^## All variables above are.*##/ d' site.bak >> \$\@-t\n"
2543                           . "\t-\@mv site.exp site.bak\n"
2544                           . "\t\@mv \$\@-t site.exp\n");
2545     }
2546     else
2547     {
2548         local ($c);
2549         foreach $c ('DEJATOOL', 'RUNTEST', 'RUNTESTFLAGS')
2550         {
2551             if (&variable_defined ($c))
2552             {
2553                 &am_line_error ($c, "\`$c' defined but \`dejagnu' not in \`AUTOMAKE_OPTIONS'");
2554             }
2555         }
2556     }
2557
2558     if (&variable_defined ('TESTS'))
2559     {
2560         push (@check, 'check-TESTS');
2561         push (@phony, 'check-TESTS');
2562         # FIXME: use $(SHELL) here?  That is what Ulrich suggests.
2563         # Maybe a new macro, $(TEST_SHELL), a la $(CONFIG_SHELL)?  For
2564         # now we just execute the file directly; this allows test
2565         # files which are compiled -- a possibly useful feature.
2566         $output_rules .= 'check-TESTS: $(TESTS)
2567         @failed=0; all=0; \\
2568         srcdir=$(srcdir); export srcdir; \\
2569         for tst in $(TESTS); do \\
2570           all=`expr $$all + 1`; \\
2571           if test -f $$tst; then dir=.; \\
2572           else dir="$(srcdir)"; fi; \\
2573           if $(TESTS_ENVIRONMENT) $$dir/$$tst; then \\
2574             echo "PASS: $$tst"; \\
2575           else \\
2576             failed=`expr $$failed + 1`; \\
2577             echo "FAIL: $$tst"; \\
2578           fi; \\
2579         done; \\
2580         if test "$$failed" -eq 0; then \\
2581           echo "========================"; \\
2582           echo "All $$all tests passed"; \\
2583           echo "========================"; \\
2584         else \\
2585           echo "$$failed of $$all tests failed"; \\
2586         fi
2587 ';
2588     }
2589 }
2590
2591 # Handle Emacs Lisp.
2592 sub handle_emacs_lisp
2593 {
2594     local (@elfiles) = &am_install_var ('lisp', 'LISP', 'lisp', 'noinst');
2595
2596     if (@elfiles)
2597     {
2598         # Found some lisp.
2599         &define_variable ("lispdir", "\@lispdir\@");
2600         $output_rules .= (".el.elc:\n"
2601                           . "\t\@echo 'WARNING: Warnings can be ignored. :-)'\n"
2602                           . "\t\$(SHELL) \$(srcdir)/elisp-comp \$<\n");
2603         push (@suffixes, '.el', '.elc');
2604
2605         # Generate .elc files.
2606         grep ($_ .= 'c', @elfiles);
2607         &define_pretty_variable ('ELCFILES', @elfiles);
2608
2609         push (@clean, 'lisp');
2610         &push_phony_cleaners ('lisp');
2611
2612         local ($varname);
2613         if (&variable_defined ('lisp_LISP'))
2614         {
2615             $varname = 'lisp_LISP';
2616             &am_error ("\`lisp_LISP' defined but \`AM_PATH_LISPDIR' not in \`configure.in'")
2617                 if ! $seen_lispdir;
2618         }
2619         else
2620         {
2621             $varname = 'noinst_LISP';
2622         }
2623
2624         &require_file_with_line ($varname, $FOREIGN, 'elisp-comp');
2625     }
2626 }
2627
2628 ################################################################
2629
2630 # Scan configure.in for interesting things.
2631 sub scan_configure
2632 {
2633     open (CONFIGURE, 'configure.in')
2634         || die "automake: couldn't open \`configure.in': $!\n";
2635     print "automake: reading configure.in\n" if $verbose;
2636
2637     # Reinitialize libsources here.  This isn't really necessary,
2638     # since we currently assume there is only one configure.in.  But
2639     # that won't always be the case.
2640     %libsources = ();
2641
2642     local ($in_ac_output, @make_list) = 0;
2643     local ($libobj_iter);
2644     while (<CONFIGURE>)
2645     {
2646         # Remove comments from current line.
2647         s/\bdnl\b.*$//;
2648         s/\#.*$//;
2649
2650         # Populate libobjs array.
2651         if (/AC_FUNC_ALLOCA/)
2652         {
2653             $libsources{'alloca.c'} = 1;
2654         }
2655         elsif (/AC_FUNC_GETLOADAVG/)
2656         {
2657             $libsources{'getloadavg.c'} = 1;
2658         }
2659         elsif (/AC_FUNC_MEMCMP/)
2660         {
2661             $libsources{'memcmp.c'} = 1;
2662         }
2663         elsif (/AC_STRUCT_ST_BLOCKS/)
2664         {
2665             $libsources{'fileblocks.c'} = 1;
2666         }
2667         elsif (/AM_FUNC_FNMATCH/)
2668         {
2669             $libsources{'fnmatch.c'} = 1;
2670         }
2671         elsif (/AC_REPLACE_FUNCS\s*\((.*)\)/)
2672         {
2673             foreach (split (' ', $1))
2674             {
2675                 $libsources{$_ . '.c'} = 1;
2676             }
2677         }
2678         elsif (/A[CM]_REPLACE_GNU_GETOPT/)
2679         {
2680             $libsources{'getopt.c'} = 1;
2681             $libsources{'getopt1.c'} = 1;
2682         }
2683         elsif (/AM_FUNC_STRTOD/)
2684         {
2685             $libsources{'strtod.c'} = 1;
2686         }
2687         elsif (/AM_WITH_REGEX/)
2688         {
2689             $libsources{'rx.c'} = 1;
2690             $libsources{'rx.h'} = 1;
2691             $libsources{'regex.c'} = 1;
2692             $libsources{'regex.h'} = 1;
2693         }
2694         elsif (/LIBOBJS="(.*)\s+\$LIBOBJS"/
2695                || /LIBOBJS="\$LIBOBJS\s+(.*)"/)
2696         {
2697             foreach $libobj_iter (split (' ', $1))
2698             {
2699                 if ($libobj_iter =~ /^(.*)\.o$/)
2700                 {
2701                     $libsources{$1 . '.c'} = 1;
2702                 }
2703             }
2704         }
2705
2706         if (/(fp_FUNC_FNMATCH|fp_PROG_INSTALL|fp_C_PROTOTYPES|jm_MAINTAINER_MODE)/)
2707         {
2708             &am_conf_line_error ($., "\`$1' is obsolete; use corresponding \`AM_' macro");
2709         }
2710
2711         # Process the AC_OUTPUT macro.
2712         if (! $in_ac_output && s/AC_OUTPUT\s*\(\[?//)
2713         {
2714             $in_ac_output = 1;
2715             $ac_output_line = $.;
2716         }
2717         if ($in_ac_output)
2718         {
2719             $in_ac_output = 0 if s/[\]\),].*$//;
2720
2721             # Look at potential Makefile.am's.
2722             foreach (split)
2723             {
2724                 next if $_ eq "\\";
2725                 if (-f $_ . '.am')
2726                 {
2727                     push (@make_list, $_);
2728                 }
2729                 else
2730                 {
2731                     push (@other_input_files, $_);
2732                 }
2733             }
2734         }
2735
2736         if (/AC_CONFIG_AUX_DIR\(([^)]+)\)/)
2737         {
2738             @config_aux_path = $1;
2739         }
2740
2741         # Check for ansi2knr.
2742         $am_c_prototypes = 1 if /AM_C_PROTOTYPES/;
2743
2744         # Check for NLS support.
2745         if (/ud_GNU_GETTEXT/)
2746         {
2747             $seen_gettext = 1;
2748             $ac_gettext_line = $.;
2749         }
2750
2751         # Look for ALL_LINGUAS.
2752         if (/ALL_LINGUAS="(.*)"$/ || /ALL_LINGUAS=(.*)$/)
2753         {
2754             $seen_linguas = 1;
2755             $all_linguas = $1;
2756             $all_linguas_line = $.;
2757         }
2758
2759         # Handle configuration headers.
2760         if (/AC_CONFIG_HEADER\s*\((.*)\)/)
2761         {
2762             $config_header_line = $.;
2763             $config_name = $1;
2764             if ($config_name =~ /^([^:]+):(.+)$/)
2765             {
2766                 $config_name = $1;
2767                 $config_header = $2;
2768             }
2769             else
2770             {
2771                 $config_header = $config_name . '.in';
2772             }
2773         }
2774
2775         # Handle AC_CANONICAL_*.  Always allow upgrading to
2776         # AC_CANONICAL_SYSTEM, but never downgrading.
2777         $seen_canonical = $AC_CANONICAL_HOST
2778             if ! $seen_canonical
2779                 && (/AC_CANONICAL_HOST/ || /AC_CHECK_TOOL/);
2780         $seen_canonical = $AC_CANONICAL_SYSTEM if /AC_CANONICAL_SYSTEM/;
2781
2782         $seen_path_xtra = 1 if /AC_PATH_XTRA/;
2783
2784         # Sometimes it is desirable to explicitly set YACC.  For
2785         # instance some people don't want to use bison.
2786         $seen_prog_yacc = 1 if (/AC_PROG_YACC/
2787                                 || /AC_SUBST\(YACC\)/
2788                                 || /AC_(PATH|CHECK)_PROGS?\(YACC/);
2789
2790         # This macro handles several different things.
2791         if (/AM_INIT_AUTOMAKE\([^,]*,([^)]+)\)/)
2792         {
2793             $seen_make_set = 1;
2794             $seen_package = 1;
2795             $seen_version = 1;
2796             $seen_arg_prog = 1;
2797             $seen_prog_install = 2;
2798             ($package_version = $1) =~ s/\s//;
2799             $package_version_line = $.;
2800         }
2801
2802         # Some things required by Automake.
2803         $seen_make_set = 1 if /AC_PROG_MAKE_SET/;
2804         $seen_arg_prog = 1 if /AC_ARG_PROGRAM/;
2805         $seen_ranlib = 1 if /AC_PROG_RANLIB/;
2806         $seen_prog_cc = 1 if /AC_PROG_CC/;
2807         $seen_prog_cxx = 1 if /AC_PROG_CXX/;
2808         $seen_prog_lex = 1 if /AC_PROG_LEX/;
2809         $seen_decl_yytext = 1 if /AC_DECL_YYTEXT/;
2810         $seen_maint_mode = 1 if /AM_MAINTAINER_MODE/;
2811         $seen_package = 1 if /PACKAGE=/;
2812
2813         if (/VERSION=(\S+)/)
2814         {
2815             $seen_version = 1;
2816             $package_version = $1;
2817             $package_version_line = $.;
2818         }
2819         elsif (/VERSION=/)
2820         {
2821             $seen_version = 1;
2822         }
2823
2824         # Weird conditionals here because it is always allowed to
2825         # upgrade to AM_PROG_INSTALL but never to downgrade to
2826         # AC_PROG_INSTALL.
2827         $seen_prog_install = 1 if ! $seen_prog_install && /AC_PROG_INSTALL/;
2828         $seen_prog_install = 2 if /AM_PROG_INSTALL/;
2829
2830         $seen_lispdir = 1 if /AM_PATH_LISPDIR/;
2831
2832         if (/AM_PROG_LIBTOOL/)
2833         {
2834             $seen_libtool = 1;
2835             $seen_ranlib = 2;
2836             $libtool_line = $.;
2837         }
2838     }
2839
2840     # Set input files if not specified by user.
2841     @input_files = @make_list if (! @input_files);
2842
2843     close (CONFIGURE);
2844
2845     &am_conf_error ("\`PACKAGE' not defined in configure.in")
2846         if ! $seen_package;
2847     &am_conf_error ("\`VERSION' not defined in configure.in")
2848         if ! $seen_version;
2849
2850     # Look for some files we need.  Always check for these.  This
2851     # check must be done for every run, even those where we are only
2852     # looking at a subdir Makefile.  We must set relative_dir so that
2853     # the file-finding machinery works.
2854     local ($relative_dir) = '.';
2855     &require_config_file ($FOREIGN, 'install-sh', 'mkinstalldirs');
2856 }
2857
2858 ################################################################
2859
2860 # Do any extra checking for GNU standards.
2861 sub check_gnu_standards
2862 {
2863     if ($relative_dir eq '.')
2864     {
2865         # In top level (or only) directory.
2866         &require_file ($GNU, 'INSTALL', 'NEWS', 'README', 'COPYING',
2867                        'AUTHORS', 'ChangeLog');
2868     }
2869
2870     if ($strictness >= $GNU)
2871     {
2872         if (defined $options{'no-installman'})
2873         {
2874             &am_line_error ('AUTOMAKE_OPTIONS',
2875                             "option \`no-installman' disallowed by GNU standards");
2876         }
2877
2878         if (defined $options{'no-installinfo'})
2879         {
2880             &am_line_error ('AUTOMAKE_OPTIONS',
2881                             "option \`no-installinfo' disallowed by GNU standards");
2882         }
2883     }
2884 }
2885
2886 # Do any extra checking for GNITS standards.
2887 sub check_gnits_standards
2888 {
2889     if ($strictness >= $GNITS)
2890     {
2891         if (-f $relative_dir . '/COPYING.LIB')
2892         {
2893             &am_error ("\`${relative_dir}/COPYING.LIB' disallowed by Gnits standards");
2894         }
2895
2896         if ($relative_dir eq '.')
2897         {
2898             if ($package_version !~ /^$GNITS_VERSION_PATTERN$/)
2899             {
2900                 &am_conf_line_error ($package_version_line,
2901                                      "version \`$package_version' doesn't follow Gnits standards");
2902             }
2903             elsif (defined $1 && -f 'README-alpha')
2904             {
2905                 # This means we have an alpha release.  See
2906                 # GNITS_VERSION_PATTERN for details.
2907                 &require_file ($GNITS, 'README-alpha');
2908             }
2909         }
2910     }
2911
2912     if ($relative_dir eq '.')
2913     {
2914         # In top level (or only) directory.
2915         &require_file ($GNITS, 'THANKS');
2916     }
2917 }
2918
2919 ################################################################
2920
2921 # Pretty-print something.  HEAD is what should be printed at the
2922 # beginning of the first line, FILL is what should be printed at the
2923 # beginning of every subsequent line.
2924 sub pretty_print_internal
2925 {
2926     local ($head, $fill, @values) = @_;
2927
2928     local ($column) = length ($head);
2929     local ($result) = $head;
2930
2931     # Fill length is number of characters.  However, each Tab
2932     # character counts for eight.  So we count the number of Tabs and
2933     # multiply by 7.
2934     local ($fill_length) = length ($fill);
2935     $fill_length += 7 * ($fill =~ tr/\t/\t/d);
2936
2937     local ($bol) = 0;
2938     foreach (@values)
2939     {
2940         # "71" because we also print a space.
2941         if ($column + length ($_) > 71)
2942         {
2943             $result .= " \\\n" . $fill;
2944             $column = $fill_length;
2945             $bol = 1;
2946         }
2947
2948         $result .= ' ' unless ($bol);
2949         $result .= $_;
2950         $column += length ($_) + 1;
2951         $bol = 0;
2952     }
2953
2954     $result .= "\n";
2955     return $result;
2956 }
2957
2958 # Pretty-print something and append to output_vars.
2959 sub pretty_print
2960 {
2961     $output_vars .= &pretty_print_internal (@_);
2962 }
2963
2964 # Pretty-print something and append to output_rules.
2965 sub pretty_print_rule
2966 {
2967     $output_rules .= &pretty_print_internal (@_);
2968 }
2969
2970
2971 ################################################################
2972
2973 # See if a variable exists.
2974 sub variable_defined
2975 {
2976     local ($var) = @_;
2977     if (defined $targets{$var})
2978     {
2979         &am_line_error ($var, "\`$var' is target; expected variable");
2980         return 0;
2981     }
2982     elsif (defined $contents{$var})
2983     {
2984         $content_seen{$var} = 1;
2985         return 1;
2986     }
2987     return 0;
2988 }
2989
2990 # Return contents of variable as list, split as whitespace.  This will
2991 # recursively follow $(...) and ${...} inclusions.  It preserves @...@
2992 # substitutions.  If PARENT is specified, it is the name of the
2993 # including variable; this is only used for error reports.
2994 sub variable_value_as_list
2995 {
2996     local ($var, $parent) = @_;
2997     local (@result);
2998
2999     if (defined $targets{$var})
3000     {
3001         &am_line_error ($var, "\`$var' is target; expected variable");
3002     }
3003     elsif (! defined $contents{$var})
3004     {
3005         &am_line_error ($parent, "variable \`$var' not defined");
3006     }
3007     else
3008     {
3009         $content_seen{$var} = 1;
3010         foreach (split (' ', $contents{$var}))
3011         {
3012             # Handle variable substitutions.
3013             if (/^\$\{(.*)\}$/ || /^\$\((.*)\)$/)
3014             {
3015                 push (@result, &variable_value_as_list ($1, $var));
3016             }
3017             else
3018             {
3019                 push (@result, $_);
3020             }
3021         }
3022     }
3023
3024     return @result;
3025 }
3026
3027 # Define a new variable, but only if not already defined.
3028 sub define_variable
3029 {
3030     local ($var, $value) = @_;
3031
3032     if (! defined $contents{$var})
3033     {
3034         $output_vars .= $var . ' = ' . $value . "\n";
3035         $contents{$var} = $value;
3036         $content_seen{$var} = 1;
3037     }
3038 }
3039
3040 # Like define_variable, but second arg is a list, and is
3041 # pretty-printed.
3042 sub define_pretty_variable
3043 {
3044     local ($var, @value) = @_;
3045     if (! defined $contents{$var})
3046     {
3047         $contents{$var} = join (' ', @value);
3048         &pretty_print ($var . ' = ', '', @value);
3049         $content_seen{$var} = 1;
3050     }
3051 }
3052
3053 # Read Makefile.am and set up %contents.  Simultaneously copy lines
3054 # from Makefile.am into $output_trailer or $output_vars as
3055 # appropriate.  NOTE we put rules in the trailer section.  We want
3056 # user rules to come after our generated stuff.
3057 sub read_am_file
3058 {
3059     local ($amfile) = @_;
3060
3061     # Compute relative location of the top object directory.
3062     local (@topdir) = ();
3063     foreach (split (/\//, $relative_dir))
3064     {
3065         next if $_ eq '.' || $_ eq '';
3066         if ($_ eq '..')
3067         {
3068             pop @topdir;
3069         }
3070         else
3071         {
3072             push (@topdir, '..');
3073         }
3074     }
3075     @topdir = ('.') if ! @topdir;
3076
3077     $top_builddir = join ('/', @topdir);
3078     local ($build_rx);
3079     ($build_rx = $top_builddir) =~ s/(\W)/\\$1/g;
3080     local ($header_vars) =
3081         &file_contents_with_transform
3082             ('s/\@top_builddir\@/' . $build_rx . '/g',
3083              'header-vars');
3084
3085     # Generate some useful variables when AC_CANONICAL_* used.
3086     if ($seen_canonical)
3087     {
3088         local ($curs, @varlist);
3089         push (@varlist, 'host_alias', 'host_triplet');
3090         if ($seen_canonical == $AC_CANONICAL_SYSTEM)
3091         {
3092             push (@varlist, 'build_alias', 'target_alias');
3093         }
3094         foreach $curs (@varlist)
3095         {
3096             $header_vars .= "$curs = \@$curs\@\n";
3097             $contents{$curs} = "\@$curs\@";
3098         }
3099     }
3100
3101     open (AM_FILE, $amfile) || die "automake: couldn't open \`$amfile': $!\n";
3102     print "automake: reading $amfile\n" if $verbose;
3103
3104     $output_vars .= ("# $in_file_name generated automatically by automake "
3105                      . $VERSION . " from $am_file_name\n");
3106
3107     # Generate copyright for generated Makefile.in.
3108     $output_vars .= $gen_copyright;
3109
3110     local ($saw_bk) = 0;
3111     local ($was_rule) = 0;
3112     local ($spacing) = '';
3113     local ($comment) = '';
3114     local ($last_var_name) = '';
3115     local ($blank) = 0;
3116
3117     while (<AM_FILE>)
3118     {
3119         if (/$IGNORE_PATTERN/o)
3120         {
3121             # Merely delete comments beginning with two hashes.
3122         }
3123         elsif (/$WHITE_PATTERN/o)
3124         {
3125             # Stick a single white line before the incoming macro or rule.
3126             $spacing = "\n";
3127             $blank = 1;
3128         }
3129         elsif (/$COMMENT_PATTERN/o)
3130         {
3131             # Stick comments before the incoming macro or rule.  Make
3132             # sure a blank line preceeds first block of comments.
3133             $spacing = "\n" unless $blank;
3134             $blank = 1;
3135             $comment .= $spacing . $_;
3136             $spacing = '';
3137         }
3138         else
3139         {
3140             last;
3141         }
3142     }
3143
3144     $output_vars .= $comment . "\n" . $header_vars;
3145     $comment = '';
3146     $spacing = "\n";
3147
3148     local ($is_ok_macro);
3149     while ($_)
3150     {
3151         $_ .= "\n"
3152             unless substr ($_, -1, 1) eq "\n";
3153
3154         $_ =~ s/\@(UN)?MAINT\@//g
3155             unless $seen_maint_mode;
3156
3157         if (/$IGNORE_PATTERN/o)
3158         {
3159             # Merely delete comments beginning with two hashes.
3160         }
3161         elsif (/$WHITE_PATTERN/o)
3162         {
3163             # Stick a single white line before the incoming macro or rule.
3164             $spacing = "\n";
3165         }
3166         elsif (/$COMMENT_PATTERN/o)
3167         {
3168             # Stick comments before the incoming macro or rule.
3169             $comment .= $spacing . $_;
3170             $spacing = '';
3171         }
3172         elsif ($saw_bk)
3173         {
3174             if ($was_rule)
3175             {
3176                 $output_trailer .= $_;
3177                 $saw_bk = /\\$/;
3178             }
3179             else
3180             {
3181                 $output_vars .= $_;
3182                 $saw_bk = /\\$/;
3183                 # Chop newline and backslash if this line is
3184                 # continued.  FIXME: maybe ensure trailing whitespace
3185                 # exists?
3186                 chop if $saw_bk;
3187                 chop if $saw_bk;
3188                 $contents{$last_var_name} .= $_;
3189             }
3190         }
3191         elsif (/$RULE_PATTERN/o)
3192         {
3193             # warn "** Saw rule .$1.\n";
3194             # Found a rule.
3195             $was_rule = 1;
3196             # Value here doesn't matter; for targets we only note
3197             # existence.
3198             $contents{$1} = 1;
3199             $targets{$1} = 1;
3200             $content_lines{$1} = $.;
3201             $output_trailer .= $comment . $spacing . $_;
3202             $comment = $spacing = '';
3203             $saw_bk = /\\$/;
3204         }
3205         elsif (($is_ok_macro = /$MACRO_PATTERN/o)
3206                || /$BOGUS_MACRO_PATTERN/o)
3207         {
3208             # Found a macro definition.
3209             $was_rule = 0;
3210             $last_var_name = $1;
3211             if (substr ($2, -1) eq "\\")
3212             {
3213                 $contents{$1} = substr ($2, 0, length ($2) - 1);
3214             }
3215             else
3216             {
3217                 $contents{$1} = $2;
3218             }
3219             $content_lines{$1} = $.;
3220             $output_vars .= $comment . $spacing . $_;
3221             $comment = $spacing = '';
3222             $saw_bk = /\\$/;
3223
3224             # Error if bogus.
3225             &am_line_error ($., "bad macro name \`$1'")
3226                 if ! $is_ok_macro;
3227         }
3228         else
3229         {
3230             # This isn't an error; it is probably a continued rule.
3231             # In fact, this is what we assume.
3232             $was_rule = 1;
3233             $output_trailer .= $comment . $spacing . $_;
3234             $comment = $spacing = '';
3235             $saw_bk = /\\$/;
3236         }
3237
3238         $_ = <AM_FILE>;
3239     }
3240
3241     $output_trailer .= $comment;
3242 }
3243
3244 ################################################################
3245
3246 sub initialize_global_constants
3247 {
3248     # Values for AC_CANONICAL_*
3249     $AC_CANONICAL_HOST = 1;
3250     $AC_CANONICAL_SYSTEM = 2;
3251
3252     # Associative array of standard directory names.  Entry is TRUE if
3253     # corresponding directory should be installed during
3254     # 'install-exec' phase.
3255     %exec_dir_p =
3256         ('bin', 1,
3257          'sbin', 1,
3258          'libexec', 1,
3259          'data', 0,
3260          'sysconf', 1,
3261          'localstate', 1,
3262          'lib', 1,
3263          'info', 0,
3264          'man', 0,
3265          'include', 0,
3266          'oldinclude', 0,
3267          'pkgdata', 0,
3268          'pkglib', 1,
3269          'pkginclude', 0
3270          );
3271
3272     # Helper text for dealing with man pages.
3273     $install_man_format =
3274     '   sect=@SECTION@;                         \\
3275         inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
3276         if test -f $(srcdir)/@MAN@; then file=$(srcdir)/@MAN@; \\
3277         else file=@MAN@; fi; \\
3278         $(INSTALL_DATA) $$file $(mandir)/man$$sect/$$inst
3279 ';
3280
3281     $uninstall_man_format =
3282     '   inst=`echo "@MANBASE@" | sed \'$(transform)\'`.@FULLSECT@; \\
3283         rm -f $(mandir)/man@SECTION@/$$inst
3284 ';
3285
3286     # Commonly found files we look for and automatically include in
3287     # DISTFILES.
3288     @common_files =
3289         (
3290          "README", "THANKS", "TODO", "NEWS", "COPYING", "COPYING.LIB",
3291          "INSTALL", "ABOUT-NLS", "ChangeLog", "configure", "configure.in",
3292          "config.guess", "config.sub", "AUTHORS", "BACKLOG", "ABOUT-GNU",
3293          "libversion.in", "mdate-sh", "mkinstalldirs", "install-sh",
3294          'texinfo.tex', "ansi2knr.c", "ansi2knr.1", 'elisp-comp',
3295          'interlock', 'ylwrap', 'acinclude.m4'
3296          );
3297
3298     # Commonly used files we auto-include, but only sometimes.
3299     @common_sometimes =
3300         (
3301          "aclocal.m4", "acconfig.h", "config.h.top",
3302          "config.h.bot", "stamp-h.in", 'stamp-vti', 'libtool'
3303          );
3304
3305     $USAGE = "\
3306   -a, --add-missing     add missing standard files to package
3307   --amdir=DIR           directory storing config files
3308   --build-dir=DIR       directory where build being done (for dependencies)
3309   --foreign             same as --strictness=foreign
3310   --gnits               same as --strictness=gnits
3311   --gnu                 same as --strictness=gnu
3312   --help                print this help, then exit
3313   -i, --include-deps    include generated dependencies in Makefile.in
3314   --no-force            only update Makefile.in's that are out of date
3315   -o DIR, --output-dir=DIR
3316                         put generated Makefile.in's into DIR
3317   --srcdir-name=DIR     name used for srcdir (for dependencies)
3318   -s LEVEL, --strictness=LEVEL
3319                         set strictness level.  LEVEL is foreign, gnu, gnits
3320   -v, --verbose         verbosely list files processed
3321   --version             print version number, then exit\n";
3322
3323     # Copyright on generated Makefile.ins.
3324     $gen_copyright = "\
3325 # Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
3326 # This Makefile.in is free software; the Free Software Foundation
3327 # gives unlimited permission to copy, distribute and modify it.
3328 ";
3329 }
3330
3331 # (Re)-Initialize per-Makefile.am variables.
3332 sub initialize_per_input
3333 {
3334     # These two variables are used when generating each Makefile.in.
3335     # They hold the Makefile.in until it is ready to be printed.
3336     $output_rules = '';
3337     $output_vars = '';
3338     $output_trailer = '';
3339
3340     # Suffixes found during a run.
3341     @suffixes = ();
3342
3343     # This holds the contents of a Makefile.am, as parsed by
3344     # read_am_file.
3345     %contents = ();
3346
3347     # This holds the names which are targets.  These also appear in
3348     # %contents.
3349     %targets = ();
3350
3351     # This holds the line numbers at which various elements of
3352     # %contents are defined.
3353     %content_lines = ();
3354
3355     # This holds a 1 if a particular variable was examined.
3356     %content_seen = ();
3357
3358     # This holds the "relative directory" of the current Makefile.in.
3359     # Eg for src/Makefile.in, this is "src".
3360     $relative_dir = '';
3361
3362     # This holds a list of files that are included in the
3363     # distribution.
3364     %dist_common = ();
3365
3366     # List of dependencies for the obvious targets.
3367     @install_data = ();
3368     @install_exec = ();
3369     @uninstall = ();
3370     @installdirs = ();
3371
3372     @info = ();
3373     @dvi = ();
3374     @all = ();
3375     @check = ();
3376     @installcheck = ();
3377     @clean = ();
3378
3379     @phony = ();
3380
3381     # These are pretty obvious, too.  They are used to define the
3382     # SOURCES and OBJECTS variables.
3383     @sources = ();
3384     @objects = ();
3385
3386     # TRUE if current directory holds any C source files.  (Actually
3387     # holds object extension, but this information is encapsulated in
3388     # the function get_object_extension).
3389     $dir_holds_sources = '';
3390
3391     # TRUE if current directory holds any headers.
3392     $dir_holds_headers = 0;
3393
3394     # TRUE if install targets should work recursively.
3395     $recursive_install = 0;
3396
3397     # All .P files.
3398     %dep_files = ();
3399
3400     # Strictness levels.
3401     $strictness = $default_strictness;
3402     $strictness_name = $default_strictness_name;
3403
3404     # Options from AUTOMAKE_OPTIONS.
3405     %options = ();
3406
3407     # Whether or not dependencies are handled.  Can be further changed
3408     # in handle_options.
3409     $use_dependencies = $cmdline_use_dependencies;
3410
3411     # Per Makefile.am.
3412     $local_maint_charset = $maint_charset;
3413
3414     # All yacc and lex source filenames for this directory.  Use
3415     # filenames instead of raw count so that multiple instances are
3416     # counted correctly (eg one yacc file can appear in multiple
3417     # programs without harm).
3418     %yacc_sources = ();
3419     %lex_sources = ();
3420
3421     # C++ source extensions we've seen.
3422     %cxx_extensions = ();
3423
3424     # TRUE if we've seen any non-C++ sources.
3425     $seen_c_source = 0;
3426 }
3427
3428
3429 ################################################################
3430
3431 # Return contents of a file from $am_dir, automatically skipping
3432 # macros or rules which are already known.  Runs command on each line
3433 # as it is read; this command can modify $_.
3434 sub file_contents_with_transform
3435 {
3436     local ($command, $basename) = @_;
3437     local ($file) = $am_dir . '/' . $basename . '.am';
3438
3439     open (FC_FILE, $file)
3440         || die "automake: installation error: cannot open \`$file'\n";
3441     # Looks stupid?
3442     # print "automake: reading $file\n" if $verbose;
3443
3444     local ($was_rule) = 0;
3445     local ($result_vars) = '';
3446     local ($result_rules) = '';
3447     local ($comment) = '';
3448     local ($spacing) = "\n";
3449     local ($skipping) = 0;
3450
3451     while (<FC_FILE>)
3452     {
3453         $_ =~ s/\@(UN)?MAINT\@//g
3454             unless $seen_maint_mode;
3455
3456         eval $command;
3457
3458         if (/$IGNORE_PATTERN/o)
3459         {
3460             # Merely delete comments beginning with two hashes.
3461         }
3462         elsif (/$WHITE_PATTERN/o)
3463         {
3464             # Stick a single white line before the incoming macro or rule.
3465             $spacing = "\n";
3466         }
3467         elsif (/$COMMENT_PATTERN/o)
3468         {
3469             # Stick comments before the incoming macro or rule.
3470             $comment .= $spacing . $_;
3471             $spacing = '';
3472         }
3473         elsif ($saw_bk)
3474         {
3475             if ($was_rule)
3476             {
3477                 $result_rules .= $_ if ! $skipping;
3478             }
3479             else
3480             {
3481                 $result_vars .= $_ if ! $skipping;
3482             }
3483             $saw_bk = /\\$/;
3484         }
3485         elsif (/$RULE_PATTERN/o)
3486         {
3487             # warn "** Found rule .$1.\n";
3488             # Found a rule.
3489             $was_rule = 1;
3490             $skipping = defined $contents{$1};
3491             # warn "** Skip $skipping\n" if $skipping;
3492             $result_rules .= $comment . $spacing . $_ if ! $skipping;
3493             $comment = $spacing = '';
3494             $saw_bk = /\\$/;
3495         }
3496         elsif (/$MACRO_PATTERN/o)
3497         {
3498             # warn "** Found macro .$1.\n";
3499             # Found a variable reference.
3500             $was_rule = 0;
3501             $skipping = defined $contents{$1};
3502             # warn "** Skip $skipping\n" if $skipping;
3503             $result_vars .= $comment . $spacing . $_ if ! $skipping;
3504             $comment = $spacing = '';
3505             $saw_bk = /\\$/;
3506         }
3507         else
3508         {
3509             # This isn't an error; it is probably a continued rule.
3510             # In fact, this is what we assume.
3511             $was_rule = 1;
3512             $result_rules .= $comment . $spacing . $_ if ! $skipping;
3513             $comment = $spacing = '';
3514             $saw_bk = /\\$/;
3515         }
3516     }
3517
3518     close (FC_FILE);
3519     return $result_vars . $result_rules . $comment;
3520 }
3521
3522 # Like file_contents_with_transform, but no transform.
3523 sub file_contents
3524 {
3525     return &file_contents_with_transform ('', @_);
3526 }
3527
3528 # Find all variable prefixes that are used for install directories.  A
3529 # prefix `zar' qualifies iff:
3530 # * `zardir' is a variable.
3531 # * `zar_PRIMARY' is a variable.
3532 sub am_primary_prefixes
3533 {
3534     local ($primary, @prefixes) = @_;
3535
3536     local (%valid, $varname);
3537     grep ($valid{$_} = 0, @prefixes);
3538     $valid{'EXTRA'} = 0;
3539     foreach $varname (keys %contents)
3540     {
3541         if ($varname =~ /^(.*)_$primary$/)
3542         {
3543             if (! defined $valid{$1} && ! &variable_defined ($1 . 'dir'))
3544             {
3545                 &am_line_error ($varname, "invalid variable \"$varname\"");
3546             }
3547             else
3548             {
3549                 # Ensure all extended prefixes are actually used.
3550                 $valid{$1} = 1;
3551             }
3552         }
3553     }
3554
3555     return %valid;
3556 }
3557
3558 # Handle `where_HOW' variable magic.  Does all lookups, generates
3559 # install code, and possibly generates code to define the primary
3560 # variable.  The first argument is the name of the .am file to munge,
3561 # the second argument is the primary variable (eg HEADERS), and all
3562 # subsequent arguments are possible installation locations.  Returns
3563 # list of all values of all _HOW targets.
3564 #
3565 # FIXME: this should be rewritten to be cleaner.  It should be broken
3566 # up into multiple functions.
3567 #
3568 # Usage is: am_install_var (OPTION..., file, HOW, where...)
3569 sub am_install_var
3570 {
3571     local (@args) = @_;
3572
3573     local ($do_all, $do_clean) = (1, 0);
3574     while (@args)
3575     {
3576         if ($args[0] eq '-clean')
3577         {
3578             $do_clean = 1;
3579         }
3580         elsif ($args[0] eq '-no-all')
3581         {
3582             $do_all = 0;
3583         }
3584         elsif ($args[0] !~ /^-/)
3585         {
3586             last;
3587         }
3588         shift (@args);
3589     }
3590     local ($file, $primary, @prefixes) = @args;
3591
3592     local (@used) = ();
3593     local (@result) = ();
3594
3595     # Now that configure substitutions are allowed in where_HOW
3596     # variables, it is an error to actually define the primary.
3597     &am_line_error ($primary, "\`$primary' is an anachronism")
3598         if &variable_defined ($primary);
3599
3600
3601     # Look for misspellings.  It is an error to have a variable ending
3602     # in a "reserved" suffix whose prefix is unknown, eg
3603     # "bni_PROGRAMS".  However, unusual prefixes are allowed if a
3604     # variable of the same name (with "dir" appended) exists.  For
3605     # instance, if the variable "zardir" is defined, then
3606     # "zar_PROGRAMS" becomes valid.  This is to provide a little extra
3607     # flexibility in those cases which need it.  Perhaps it should be
3608     # disallowed in the Gnits case?  The problem is, sometimes it is
3609     # useful to put things in a subdir of eg pkgdatadir, perhaps even
3610     # for Gnitsoids.
3611     local (%valid) = &am_primary_prefixes ($primary, @prefixes);
3612
3613     local ($clean_file) = $file . '-clean';
3614     local ($one_name);
3615     local ($X);
3616     foreach $X (keys %valid)
3617     {
3618         $one_name = $X . '_' . $primary;
3619         if (&variable_defined ($one_name))
3620         {
3621             # Append actual contents of where_PRIMARY variable to
3622             # result.
3623             local ($rcurs);
3624             foreach $rcurs (&variable_value_as_list ($one_name))
3625             {
3626                 # Skip configure substitutions.  Possibly bogus.
3627                 next if $rcurs =~ /^\@.*\@$/;
3628                 push (@result, $rcurs);
3629             }
3630
3631             # "EXTRA" shouldn't be used when generating clean targets,
3632             # @all, or install targets.
3633             next if $X eq 'EXTRA';
3634
3635             if ($do_clean)
3636             {
3637                 $output_rules .=
3638                     &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go',
3639                                                    $clean_file);
3640
3641                 push (@clean, $X . $primary);
3642                 &push_phony_cleaners ($X . $primary);
3643             }
3644
3645             if ($X eq 'check')
3646             {
3647                 push (@check, '$(' . $one_name . ')');
3648             }
3649             else
3650             {
3651                 push (@used, '$(' . $one_name . ')');
3652             }
3653             if ($X eq 'noinst' || $X eq 'check')
3654             {
3655                 # Objects which don't get installed by default.
3656                 next;
3657             }
3658
3659             $output_rules .=
3660                 &file_contents_with_transform ('s/\@DIR\@/' . $X . '/go',
3661                                                $file);
3662
3663             push (@uninstall, 'uninstall-' . $X . $primary);
3664             push (@phony, 'uninstall-' . $X . $primary);
3665             push (@installdirs, '$(' . $X . 'dir)');
3666             if ($exec_dir_p{$X})
3667             {
3668                 push (@install_exec, 'install-' . $X . $primary);
3669                 push (@phony, 'install-' . $X . $primary);
3670             }
3671             else
3672             {
3673                 push (@install_data, 'install-' . $X . $primary);
3674                 push (@phony, 'install-' . $X . $primary);
3675             }
3676         }
3677     }
3678
3679     if (@used)
3680     {
3681         # Define it.
3682         &define_pretty_variable ($primary, @used);
3683         $output_vars .= "\n";
3684     }
3685
3686     # Push here because PRIMARY might be configure time determined.
3687     push (@all, '$(' . $primary . ')')
3688         if $do_all && @used;
3689
3690     return (@result);
3691 }
3692
3693
3694 ################################################################
3695
3696 # This variable is local to the "require file" set of functions.
3697 @require_file_paths = ();
3698
3699 # Verify that the file must exist in the current directory.  Usage:
3700 # require_file (isconfigure, line_number, strictness, file) strictness
3701 # is the strictness level at which this file becomes required.  Must
3702 # set require_file_paths before calling this function.
3703 # require_file_paths is set to hold a single directory (the one in
3704 # which the first file was found) before return.
3705 sub require_file_internal
3706 {
3707     local ($is_configure, $line, $mystrict, @files) = @_;
3708     local ($file, $fullfile);
3709     local ($found_it, $errfile, $errdir);
3710     local ($save_dir);
3711
3712     foreach $file (@files)
3713     {
3714         $found_it = 0;
3715         foreach $dir (@require_file_paths)
3716         {
3717             if ($dir eq '.')
3718             {
3719                 $fullfile = $relative_dir . "/" . $file;
3720                 $errdir = $relative_dir unless $errdir;
3721             }
3722             else
3723             {
3724                 $fullfile = $dir . "/" . $file;
3725                 $errdir = $dir unless $errdir;
3726             }
3727
3728             # Use different name for "error filename".  Otherwise on
3729             # an error the bad file will be reported as eg
3730             # `../../install-sh' when using the default
3731             # config_aux_path.
3732             $errfile = $errdir . '/' . $file;
3733
3734             if (-f $fullfile)
3735             {
3736                 $found_it = 1;
3737                 # FIXME: Once again, special-case `.'.
3738                 &push_dist_common ($file)
3739                     if $dir eq $relative_dir || $dir eq '.';
3740                 $save_dir = $dir;
3741                 last;
3742             }
3743         }
3744
3745         if ($found_it)
3746         {
3747             # Prune the path list.
3748             @require_file_paths = $save_dir;
3749         }
3750         else
3751         {
3752             if ($strictness >= $mystrict)
3753             {
3754                 # Only install missing files according to our desired
3755                 # strictness level.
3756                 if ($add_missing && -f ($am_dir . '/' . $file))
3757                 {
3758                     # Install the missing file.  Symlink if we can, copy
3759                     # if we must.
3760                     if ($symlink_exists)
3761                     {
3762                         symlink ($am_dir . '/' . $file, $errfile);
3763                     }
3764                     else
3765                     {
3766                         system ('cp', $am_dir . '/' . $file, $errfile);
3767                     }
3768
3769                     # FIXME: this is a hack.  Should have am_warn.
3770                     local ($save) = $exit_status;
3771                     if ($is_configure)
3772                     {
3773                         &am_conf_line_error
3774                             ($line,
3775                              "required file \"$errfile\" not found; installing");
3776                     }
3777                     else
3778                     {
3779                         &am_line_error
3780                             ($line,
3781                              "required file \"$errfile\" not found; installing");
3782                     }
3783                     $exit_status = $save;
3784                 }
3785                 else
3786                 {
3787                     # Only an error if strictness constraint violated.
3788                     if ($is_configure)
3789                     {
3790                         &am_conf_line_error
3791                             ($line, "required file \"$errfile\" not found");
3792                     }
3793                     else
3794                     {
3795                         &am_line_error
3796                             ($line, "required file \"$errfile\" not found");
3797                     }
3798                 }
3799             }
3800         }
3801     }
3802 }
3803
3804 # Like require_file_with_line, but error messages refer to
3805 # configure.in, not the current Makefile.am.
3806 sub require_file_with_conf_line
3807 {
3808     @require_file_paths = '.';
3809     &require_file_internal (1, @_);
3810 }
3811
3812 sub require_file_with_line
3813 {
3814     @require_file_paths = '.';
3815     &require_file_internal (0, @_);
3816 }
3817
3818 sub require_file
3819 {
3820     @require_file_paths = '.';
3821     &require_file_internal (0, '', @_);
3822 }
3823
3824 # Require a file that is also required by Autoconf.  Looks in
3825 # configuration path, as specified by AC_CONFIG_AUX_DIR.
3826 sub require_config_file
3827 {
3828     @require_file_paths = @config_aux_path;
3829     &require_file_internal (0, '', @_);
3830     local ($dir) = $require_file_paths[0];
3831     @config_aux_path = @require_file_paths;
3832     if ($dir eq '.')
3833     {
3834         $config_aux_dir = '.';
3835     }
3836     else
3837     {
3838         $config_aux_dir = '$(top_srcdir)/' . $dir;
3839     }
3840 }
3841
3842 # Assumes that the line number is in Makefile.am.
3843 sub require_conf_file_with_line
3844 {
3845     @require_file_paths = @config_aux_path;
3846     &require_file_internal (0, @_);
3847     local ($dir) = $require_file_paths[0];
3848     @config_aux_path = @require_file_paths;
3849     if ($dir eq '.')
3850     {
3851         $config_aux_dir = '.';
3852     }
3853     else
3854     {
3855         $config_aux_dir = '$(top_srcdir)/' . $dir;
3856     }
3857 }
3858
3859 # Assumes that the line number is in Makefile.am.
3860 sub require_conf_file_with_conf_line
3861 {
3862     @require_file_paths = @config_aux_path;
3863     &require_file_internal (1, @_);
3864     local ($dir) = $require_file_paths[0];
3865     @config_aux_path = @require_file_paths;
3866     if ($dir eq '.')
3867     {
3868         $config_aux_dir = '.';
3869     }
3870     else
3871     {
3872         $config_aux_dir = '$(top_srcdir)/' . $dir;
3873     }
3874 }
3875
3876 ################################################################
3877
3878 # Push a list of files onto dist_common.
3879 sub push_dist_common
3880 {
3881     local (@files) = @_;
3882     local ($file);
3883
3884     foreach $file (@files)
3885     {
3886         $dist_common{$file} = 1;
3887     }
3888 }
3889
3890 # Push a list of clean targets onto phony.
3891 sub push_phony_cleaners
3892 {
3893     local ($base) = @_;
3894     local ($target);
3895     foreach $target ('mostly', 'dist', '', 'maintainer-')
3896     {
3897         push (@phony, $target . 'clean-' . $base);
3898     }
3899 }
3900
3901 # Set strictness.
3902 sub set_strictness
3903 {
3904     $strictness_name = $_[0];
3905     if ($strictness_name eq 'gnu')
3906     {
3907         $strictness = $GNU;
3908     }
3909     elsif ($strictness_name eq 'gnits')
3910     {
3911         $strictness = $GNITS;
3912     }
3913     elsif ($strictness_name eq 'foreign')
3914     {
3915         $strictness = $FOREIGN;
3916     }
3917     else
3918     {
3919         die "automake: level \`$strictness_name' not recognized\n";
3920     }
3921 }
3922
3923
3924 ################################################################
3925
3926 # Return directory name of file.
3927 sub dirname
3928 {
3929     local ($file) = @_;
3930     local ($sub);
3931
3932     ($sub = $file) =~ s,/+[^/]+$,,g;
3933     $sub = '.' if $sub eq $file;
3934     return $sub;
3935 }
3936
3937 ################################################################
3938
3939 # Print an error message and set exit status.
3940 sub am_error
3941 {
3942     warn "automake: ${am_file}.am: ", join (' ', @_), "\n";
3943     $exit_status = 1;
3944 }
3945
3946 sub am_line_error
3947 {
3948     local ($symbol, @args) = @_;
3949
3950     if ($symbol)
3951     {
3952         # If SYMBOL not already a line number, look it up in Makefile.am.
3953         $symbol = $content_lines{$symbol} unless $symbol =~ /^\d+$/;
3954         $symbol .= ': ' if $symbol;
3955         warn "${am_file}.am:", $symbol, join (' ', @args), "\n";
3956         $exit_status = 1;
3957     }
3958     else
3959     {
3960         &am_error (@args);
3961     }
3962 }
3963
3964 # Like am_error, but while scanning configure.in.
3965 sub am_conf_error
3966 {
3967     # FIXME: can run in subdirs.
3968     warn "automake: configure.in: ", join (' ', @_), "\n";
3969     $exit_status = 1;
3970 }
3971
3972 # Error message with line number referring to configure.in.
3973 sub am_conf_line_error
3974 {
3975     local ($line, @args) = @_;
3976
3977     if ($line)
3978     {
3979         warn "configure.in: $line: ", join (' ', @args), "\n";
3980         $exit_status = 1;
3981     }
3982     else
3983     {
3984         &am_conf_error (@args);
3985     }
3986 }
3987
3988 # Tell user where our aclocal.m4 is, but only once.
3989 sub keyed_aclocal_warning
3990 {
3991     local ($key) = @_;
3992     warn "automake: macro \`$key' can be generated by \`aclocal'\n";
3993 }
3994
3995 # Print usage information.
3996 sub usage
3997 {
3998     print "Usage: automake [OPTION] ... [Makefile]...\n";
3999     print $USAGE;
4000     print "\nFiles which are automatically distributed, if found:\n";
4001     $~ = "USAGE_FORMAT";
4002     local (@lcomm) = sort ((@common_files, @common_sometimes));
4003     local ($one, $two, $three, $four);
4004     while (@lcomm > 0)
4005     {
4006         $one = shift @lcomm;
4007         $two = @lcomm ? shift @lcomm : '';
4008         $three = @lcomm ? shift @lcomm : '';
4009         $four = @lcomm ? shift @lcomm : '';
4010         write;
4011     }
4012
4013     print "\nReport bugs to <bug-gnu-utils\@prep.ai.mit.edu>\n";
4014
4015     exit 0;
4016 }
4017
4018 format USAGE_FORMAT =
4019   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<
4020   $one,               $two,               $three,             $four
4021 .