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