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